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 ] ; } } char [ ] newStr = new char [ endIndex ] ; Array . Copy ( chars , newStr , endIndex ) ; return newStr ; }