Java Program To Remove Duplicates From A Given String
Given a 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.
METHOD 1 (Simple)
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:
geksfor
Time Complexity : O(n * n)
Auxiliary Space : O(1)
Keeps order of elements the same as input.
METHOD 2 (Use BST)
use set which implements a self-balancing Binary Search Tree.
Java
// Java program to remove duplicate character // from character array and print in sorted // order import java.util.*; class GFG { static void removeDuplicate( char str[], int n) { // Create a set using String characters // excluding '�' HashSet<Character> s = new LinkedHashSet<>(n - 1 ); // HashSet doesn't allow repetition of elements for ( char x : str) s.add(x); // Print content of the set for ( char x : s) System.out.print(x); } // Driver code public static void main(String[] args) { char str[] = "geeksforgeeks" .toCharArray(); int n = str.length; removeDuplicate(str, n); } } // This code is contributed by todaysgaurav |
Output:
efgkors
Time Complexity: O(n Log n)
Auxiliary Space: O(n)
Thanks to Anivesh Tiwari for suggesting this approach.
It does not keep the order of elements the same as the input but prints them in sorted order.
METHOD 3 (Use Sorting)
Algorithm:
1) Sort the elements. 2) Now in a loop, remove duplicates by comparing the current character with previous character. 3) Remove extra characters at the end of the resultant string.
Example:
Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors
Note that, this method doesn’t keep the original order of the input string. For example, if we are to remove duplicates for geeksforgeeks and keep the order of characters the same, then the output should be geksfor, but the above function returns efgkos. We can modify this method by storing the original order.
Implementation:
Java
// Java program to remove duplicates, the order of // characters is not maintained in this program import java.util.Arrays; public class GFG { /* Method to remove duplicates in a sorted array */ static String removeDupsSorted(String str) { int res_ind = 1 , ip_ind = 1 ; // Character array for removal of duplicate characters char arr[] = str.toCharArray(); /* In place removal of duplicate characters*/ while (ip_ind != arr.length) { if (arr[ip_ind] != arr[ip_ind- 1 ]) { arr[res_ind] = arr[ip_ind]; res_ind++; } ip_ind++; } str = new String(arr); return str.substring( 0 ,res_ind); } /* Method removes duplicate characters from the string This function work in-place and fills null characters in the extra space left */ static String removeDups(String str) { // Sort the character array char temp[] = str.toCharArray(); Arrays.sort(temp); str = new String(temp); // Remove duplicates from sorted return removeDupsSorted(str); } // Driver Method public static void main(String[] args) { String str = "geeksforgeeks" ; System.out.println(removeDups(str)); } } |
Output:
efgkors
Time Complexity: O(n log n) If we use some nlogn sorting algorithm instead of quicksort.
Auxiliary Space: O(1)
METHOD 4 (Use Hashing )
Algorithm:
1: Initialize: str = "test string" /* input string */ ip_ind = 0 /* index to keep track of location of next character in input string */ res_ind = 0 /* index to keep track of location of next character in the resultant string */ bin_hash[0..255] = {0,0, ….} /* Binary hash to see if character is already processed or not */ 2: Do following for each character *(str + ip_ind) in input string: (a) if bin_hash is not set for *(str + ip_ind) then // if program sees the character *(str + ip_ind) first time (i) Set bin_hash for *(str + ip_ind) (ii) Move *(str + ip_ind) to the resultant string. This is done in-place. (iii) res_ind++ (b) ip_ind++ /* String obtained after this step is "te stringing" */ 3: Remove extra characters at the end of the resultant string. /* String obtained after this step is "te string" */
Implementation:
Java
// Java program to remove duplicates import java.util.*; class RemoveDuplicates { /* Function removes duplicate characters from the string This function work in-place */ void removeDuplicates(String str) { LinkedHashSet<Character> lhs = new LinkedHashSet<>(); for ( int i= 0 ;i<str.length();i++) lhs.add(str.charAt(i)); // print string after deleting duplicate elements for (Character ch : lhs) System.out.print(ch); } /* Driver program to test removeDuplicates */ public static void main(String args[]) { String str = "geeksforgeeks" ; RemoveDuplicates r = new RemoveDuplicates(); r.removeDuplicates(str); } } // This code has been contributed by Amit Khandelwal (Amit Khandelwal 1) |
Output:
geksfor
Time Complexity: O(n)
The time complexity of this approach is O(n) as for loop is used to iterate over each character of the string.
Space complexity: O(n).
The space complexity of this approach is O(n) as a LinkedHashSet is used to store the characters of the string.
Important Points:
- Method 2 doesn’t maintain the characters as original strings, but method 4 does.
- It is assumed that the number of possible characters in the input string is 256. NO_OF_CHARS should be changed accordingly.
- calloc() is used instead of malloc() for memory allocations of a counting array (count) to initialize allocated memory to ‘�’. the malloc() followed by memset() could also be used.
- The above algorithm also works for integer array inputs if the range of the integers in the array is given. An example problem is to find the maximum occurring number in an input array given that the input array contains integers only between 1000 to 1100
Method 5 (Using IndexOf() method) :
Prerequisite : Java IndexOf() method
Java
// Java program to create a unique string import java.util.*; class IndexOf { // Function to make the string unique public static String unique(String s) { String str = new String(); int len = s.length(); // loop to traverse the string and // check for repeating chars using // IndexOf() method in Java for ( int i = 0 ; i < len; i++) { // character at i'th index of s char c = s.charAt(i); // if c is present in str, it returns // the index of c, else it returns -1 if (str.indexOf(c) < 0 ) { // adding c to str if -1 is returned str += c; } } return str; } // Driver code public static void main(String[] args) { // Input string with repeating chars String s = "geeksforgeeks" ; System.out.println(unique(s)); } } |
Output:
geksfor
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n), where n is the length of the string. We are creating a new string to store the unique characters.
Thanks debjitdbb for suggesting this approach.
Method 6 (Using unordered_map STL method) :
Prerequisite : unordered_map STL C++ method
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)); } } // This code is contributed by gauravrajput1 |
Output:
geksfor
Time Complexity : O(n)
Auxiliary Space : O(n)
Thanks, Allen James Vinoy for suggesting this approach.
Please refer complete article on Remove duplicates from a given string for more details!
Please Login to comment...