Remove duplicates from a string in O ( 1 ) extra space | Method to remove duplicates ; table to keep track of visited characters ; to keep track of end index of resultant string ; Driver code
function removeDuplicatesFromString ( string ) { let table = new Array ( 256 ) ; for ( let i = 0 ; i < table . length ; i ++ ) table [ i ] = 0 ; let chars = string . split ( " " ) ; let endIndex = 0 ; for ( let i = 0 ; i < chars . length ; i ++ ) { if ( table [ chars [ i ] . charCodeAt ( 0 ) ] == 0 ) { table [ chars [ i ] . charCodeAt ( 0 ) ] = - 1 ; chars [ endIndex ++ ] = chars [ i ] ; } } let ans = " " ; for ( let i = 0 ; i < endIndex ; i ++ ) ans += chars [ i ] return ans ; } let str = " " ; document . write ( removeDuplicatesFromString ( str ) ) ;