a | C # program to remove consecutive duplicates from a given string . ; A iterative function that removes consecutive duplicates from string S ; We don 't need to do anything for  empty or single character string. ; j is used to store index is result string ( or index of current distinct character ) ; Traversing string ; If current character S [ i ] is different from S [ j ] ; Driver code
using System ; class GFG { static void removeDuplicates ( char [ ] S ) { int n = S . Length ; if ( n < 2 ) { return ; } int j = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S [ j ] != S [ i ] ) { j ++ ; S [ j ] = S [ i ] ; } } char [ ] A = new char [ j + 1 ] ; Array . Copy ( S , 0 , A , 0 , j + 1 ) ; Console . WriteLine ( A ) ; } public static void Main ( String [ ] args ) { char [ ] S1 = " geeksforgeeks " . ToCharArray ( ) ; removeDuplicates ( S1 ) ; char [ ] S2 = " aabcca " . ToCharArray ( ) ; removeDuplicates ( S2 ) ; } }