a | Java 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
import java . util . * ; 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 ] ; } } System . out . println ( Arrays . copyOfRange ( S , 0 , j + 1 ) ) ; } public static void main ( String [ ] args ) { char S1 [ ] = " geeksforgeeks " . toCharArray ( ) ; removeDuplicates ( S1 ) ; char S2 [ ] = " aabcca " . toCharArray ( ) ; removeDuplicates ( S2 ) ; } }