Append digits to the end of duplicate strings to make all strings in an array unique | Function to replace duplicate strings by alphanumeric strings to make all strings in the array unique ; Store the frequency of strings ; Iterate over the array ; For the first occurrence , update the frequency count ; Otherwise ; Append frequency count to end of the string ; Print the modified array ; Driver Code ; Function Call
function replaceDuplicates ( names ) { var hash = new Map ( ) ; for ( var i = 0 ; i < names . length ; i ++ ) { if ( ! hash . has ( names [ i ] ) ) hash . set ( names [ i ] , 1 ) ; else { var count = hash . get ( names [ i ] ) ; hash . set ( names [ i ] , hash . get ( names [ i ] ) + 1 ) ; names [ i ] += count . toString ( ) ; } } for ( var i = 0 ; i < names . length ; i ++ ) { document . write ( names [ i ] + " " ) ; } } var str = [ " " , " " , " " , " " , " " , " " , " " ] ; replaceDuplicates ( str ) ;