Remove duplicates from a string in O ( 1 ) extra space | Java implementation of above approach ; Method to remove duplicates ; table to keep track of visited characters ; to keep track of end index of resultant string ; Driver code
import java . util . Arrays ; class GFG { static char [ ] removeDuplicatesFromString ( String string ) { int [ ] table = new int [ 256 ] ; char [ ] chars = string . toCharArray ( ) ; int endIndex = 0 ; for ( int i = 0 ; i < chars . length ; i ++ ) { if ( table [ chars [ i ] ] == 0 ) { table [ chars [ i ] ] = - 1 ; chars [ endIndex ++ ] = chars [ i ] ; } } return Arrays . copyOfRange ( chars , 0 , endIndex ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( removeDuplicatesFromString ( str ) ) ; } }