Remove duplicates from a string in O ( 1 ) extra space | C # 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
using System ; class GFG { static char [ ] removeDuplicatesFromString ( String str ) { int [ ] table = new int [ 256 ] ; char [ ] chars = str . 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 ] ; } } char [ ] newStr = new char [ endIndex ] ; Array . Copy ( chars , newStr , endIndex ) ; return newStr ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; Console . WriteLine ( removeDuplicatesFromString ( str ) ) ; } }