How to Remove Duplicates from a String in Java? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. In this article we have given a string, the task is to remove duplicates from it. Example Input: s = "geeks for geeks"Output: str = "geks for" Remove Duplicates From a String in JavaThis approach uses a for loop to check for duplicate characters, the code checks for the presence of spaces and non-space characters. If the character is not a space, the code executes further to check whether the character is already there in the ans string or not, this is checked using the indexOf() method, which returns the first occurrence index of the character if the character is present, else returns -1. If the character is not there it gets added to the ans string. The final answer is trimmed using the trim() method to remove extra leading and trailing spaces. Below is the implementation of removing duplicates from a String: Java // Java Program to Remove Duplicates // From a String import java.util.*; // Driver Class class GFG { public static void main(String[] args) { String s = "geeks for geeks"; String ans = ""; // using a for loop to iterate in the string for (int i = 0; i < s.length(); i++) { char temp = s.charAt(i); // checking for space in the string if (temp != ' ') { // checking if the character is already // present in the new String if not adding // the character to the new string if (ans.indexOf(temp) <= -1) { ans = ans + temp; } } // if there is a space adding that to the string else { ans = ans + ' '; } } // using trim function to remove if any leading and // trailing space are there ans = ans.trim(); System.out.println("Output : " + ans); } } OutputOutput : geks for Comment More infoAdvertise with us Next Article How to Remove Duplicates from a String in Java? P priyasha2323 Follow Improve Article Tags : Java Java Programs Java-Strings strings Java Examples +1 More Practice Tags : JavaJava-StringsStrings Similar Reads How to Remove Duplicates from a String in Java Using Regex ? In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in J 2 min read How to Remove Duplicates from a String in Java Using Hashing? Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. Using hashing is one effective way to do this. By assigning a unique hash code to each element, hashing enables us to keep track of unique items. This article will exam 2 min read How to find duplicate elements in a Stream in Java Given a stream containing some elements, the task is to find the duplicate elements in this stream in Java. Examples: Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34} Output: [59, 13] Explanation: The only duplicate elements in the given stream are 59 and 13. Input: Stream = {5, 13, 4, 21, 27, 5 min read Java Program to Remove a Given Word From a String Given a string and a word that task to remove the word from the string if it exists otherwise return -1 as an output. For more clarity go through the illustration given below as follows. Illustration: Input : This is the Geeks For Geeks word="the" Output : This is Geeks For Geeks Input : Hello world 3 min read How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read Like