Java Program To Remove Duplicates From A Given String Last Updated : 13 Oct, 2023 Comments Improve Suggest changes Like Article Like Report Write a java program for a given string S, the task is to remove all the duplicates in the given string. Below are the different methods to remove duplicates in a string. Note: The order of remaining characters in the output should be the same as in the original string.Example: Input: Str = geeksforgeeksOutput: geksforExplanation: After removing duplicate characters such as e, k, g, s, we have string as “geksfor”. Input: Str = HappyNewYearOutput: HapyNewYrExplanation: After removing duplicate characters such as p, e, a, we have string as “HapyNewYr”. Naive Approach: Iterate through the string and for each character check if that particular character has occurred before it in the string. If not, add the character to the result, otherwise the character is not added to result. Below is the implementation of above approach: Java // Java program to remove duplicate character // from character array and print in sorted // order import java.util.*; class GFG { static String removeDuplicate(char str[], int n) { // Used as index in the modified string int index = 0; // Traverse through all characters for (int i = 0; i < n; i++) { // Check if str[i] is present before it int j; for (j = 0; j < i; j++) { if (str[i] == str[j]) { break; } } // If not present, then add it to // result. if (j == i) { str[index++] = str[i]; } } return String.valueOf(Arrays.copyOf(str, index)); } // Driver code public static void main(String[] args) { char str[] = "geeksforgeeks".toCharArray(); int n = str.length; System.out.println(removeDuplicate(str, n)); } } // This code is contributed by Rajput-Ji Output: geksforTime Complexity : O(n * n) Auxiliary Space : O(1) , Keeps order of elements the same as input. Remove duplicates from a given string using HashingIterating through the given string and use a map to efficiently track of encountered characters. If a character is encountered for the first time, it’s added to the result string, Otherwise, it’s skipped. This ensures the output string contains only unique characters in the same order as the input string. Below is the implementation of above approach: Java // Java program to create a unique String using // unordered_map /* access time in unordered_map on is O(1) generally if no collisions occur and therefore it helps us check if an element exists in a String in O(1) time complexity with constant space. */ import java.util.*; class GFG { static char[] removeDuplicates(char[] s, int n) { Map<Character, Integer> exists = new HashMap<>(); String st = ""; for (int i = 0; i < n; i++) { if (!exists.containsKey(s[i])) { st += s[i]; exists.put(s[i], 1); } } return st.toCharArray(); } // driver code public static void main(String[] args) { char s[] = "geeksforgeeks".toCharArray(); int n = s.length; System.out.print(removeDuplicates(s, n)); } } Output: geksforTime Complexity: O(n)Space complexity: O(n) Please refer complete article on Remove duplicates from a given string for more details! Comment More infoAdvertise with us Next Article Java Program To Remove Duplicates From A Given String kartik Follow Improve Article Tags : Strings Java Programs DSA frequency-counting Practice Tags : Strings Similar Reads 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 Duplicates from a String in Java? 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 2 min read 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 Java Program to Remove Duplicate Elements From the Array Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted.Example:Java// Java Program to Remove Duplicate // Ele 6 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 Java Program to Remove Duplicate Entries from an Array using TreeSet Features of TreeSet is the primary concern it is widely used in remove duplicates in the data structure as follows: TreeSet implements the SortedSet interface. So, duplicate values are not allowed and will be leftovers.Objects in a TreeSet are stored in a sorted and ascending order.TreeSet does not 3 min read Java Program To Remove All The Duplicate Entries From The Collection As we know that the HashSet contains only unique elements, ie no duplicate entries are allowed, and since our aim is to remove the duplicate entries from the collection, so for removing all the duplicate entries from the collection, we will use HashSet.The HashSet class implements the Set interface, 3 min read Java Program For Removing Duplicates From An Unsorted Linked List Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 -> 6 min read Java program to print all duplicate characters in a string Given a string, the task is to write Java program to print all the duplicate characters with their frequency Example: Input: str = "geeksforgeeks" Output: s : 2 e : 4 g : 2 k : 2 Input: str = "java" Output: a : 2 Approach: The idea is to do hashing using HashMap. Create a hashMap of type {char, int} 2 min read Java Program to Find Duplicate Words in a Regular Expression Given an Expression which is represented by String. The task is to find duplicate elements in a Regular Expression in Java. Use a map or set data structures for identifying the uniqueness of words in a sentence. Examples: Input : str = " Hi, I am Hritik and I am a programmer. " Output: I am Explanat 3 min read Like