Remove duplicates from string keeping the order according to last occurrences | Javascript program to remove duplicate character from character array and print in 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
function removeDuplicates ( str ) { var n = str . length ; var s = new Set ( ) ; var res = " " ; for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( ! s . has ( str [ i ] ) ) { res = res + str [ i ] ; s . add ( str [ i ] ) ; } } res = res . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; return res ; } var str = " " ; document . write ( removeDuplicates ( str ) ) ;