Append digits to the end of duplicate strings to make all strings in an array unique | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void replaceDuplicates ( vector < string > & names ) { unordered_map < string , int > hash ; for ( int i = 0 ; i < names . size ( ) ; i ++ ) { if ( hash . count ( names [ i ] ) == 0 ) hash [ names [ i ] ] ++ ; else { int count = hash [ names [ i ] ] ++ ; names [ i ] += to_string ( count ) ; } } for ( int i = 0 ; i < names . size ( ) ; i ++ ) { cout << names [ i ] << " ▁ " ; } } int main ( ) { vector < string > str = { " aa " , " bb " , " cc " , " bb " , " aa " , " aa " , " aa " } ; replaceDuplicates ( str ) ; return 0 ; }