to keep track of end index of resultant string
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 ; }