Remove duplicates from a given string | 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 . ; driver code
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 ( ) ; } public static void main ( String [ ] args ) { char s [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = s . length ; System . out . print ( removeDuplicates ( s , n ) ) ; } }