Remove duplicates from string keeping the order according to last occurrences | Python3 program to remove duplicate character from character array and prin sorted order ; Used as index in the modified string ; Create an empty hash table ; Traverse through all characters from right to left ; If current character is not in ; Reverse the result string ; Driver code
def removeDuplicates ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE s = set ( ) NEW_LINE res = " " NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] not in s ) : NEW_LINE INDENT res = res + str [ i ] NEW_LINE s . add ( str [ i ] ) NEW_LINE DEDENT DEDENT res = res [ : : - 1 ] NEW_LINE return res NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE print ( removeDuplicates ( str ) ) NEW_LINE