Remove duplicates from a string in O ( 1 ) extra space | Java implementation of above approach ; Function to remove duplicates ; keeps track of visited characters ; gets character value ; keeps track of length of resultant String ; check if Xth bit of counter is unset ; mark current character as visited ; Driver code
import java . util . Arrays ; class GFG { static char [ ] removeDuplicatesFromString ( String string ) { int counter = 0 ; char [ ] str = string . toCharArray ( ) ; int i = 0 ; int size = str . length ; int x ; int length = 0 ; while ( i < size ) { x = str [ i ] - 97 ; if ( ( counter & ( 1 << x ) ) == 0 ) { str [ length ] = ( char ) ( ' a ' + x ) ; counter = counter | ( 1 << x ) ; length ++ ; } i ++ ; } return Arrays . copyOfRange ( str , 0 , length ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( removeDuplicatesFromString ( str ) ) ; } }