a | 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
def removeDuplicates ( S ) : NEW_LINE INDENT n = len ( S ) NEW_LINE if ( n < 2 ) : NEW_LINE INDENT return NEW_LINE DEDENT j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( S [ j ] != S [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE S [ j ] = S [ i ] NEW_LINE DEDENT DEDENT j += 1 NEW_LINE S = S [ : j ] NEW_LINE return S NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = " geeksforgeeks " NEW_LINE S1 = list ( S1 . rstrip ( ) ) NEW_LINE S1 = removeDuplicates ( S1 ) NEW_LINE print ( * S1 , sep = " " ) NEW_LINE S2 = " aabcca " NEW_LINE S2 = list ( S2 . rstrip ( ) ) NEW_LINE S2 = removeDuplicates ( S2 ) NEW_LINE print ( * S2 , sep = " " ) NEW_LINE DEDENT