How to Remove Duplicates from a String in Java? Last Updated : 23 Jul, 2025 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 Create Quiz Comment P priyasha2323 Follow 0 Improve P priyasha2323 Follow 0 Improve Article Tags : Java Java Programs Java-Strings strings Java Examples +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like