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