Remove duplicates from a string in O ( 1 ) extra space | C # implementation of above approach ; Function to remove duplicates ; keeps track of visited characters ; gets character value ; keeps track of length of resultant String ; check if Xth bit of counter is unset ; mark current character as visited ; Driver code
using System ; class GFG { static string removeDuplicatesFromString ( string string1 ) { int counter = 0 ; char [ ] str = string1 . ToCharArray ( ) ; int i = 0 ; int size = str . Length ; int x ; int length = 0 ; while ( i < size ) { x = str [ i ] - 97 ; if ( ( counter & ( 1 << x ) ) == 0 ) { str [ length ] = ( char ) ( ' a ' + x ) ; counter = counter | ( 1 << x ) ; length ++ ; } i ++ ; } return ( new string ( str ) ) . Substring ( 0 , length ) ; } static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( removeDuplicatesFromString ( str ) ) ; } }