My goal is to turn
['sara', 'mike', 'sara','sara','Jon']
into
'sara', 'mike', 'sara(1)','sara(2)' 'Jon'
Ive seen some solutions when counting the total number of duplicates, however, couldnt find anything for what I am trying to do..
ps. I cannot use sort() as the names should stay where they were in the original array..
I tried using a regular for loop but nothing really worked, I also think a map approach would probably be good, but cant figure out how to do that.
Thank you!
EDIT:
I actually came up with this here:
function words(arr){
var result=[];
var count=0;
for (var i=0; i<arr.length; i++){
if (result.indexOf(arr[i])==-1){
result.push(arr[i]);
}
else {
count++;
result.push(arr[i]+"("+count+")");
}
}
return result.join(',');
}
words(['sara', 'mike', 'sara','sara','Jon'] );
and it works except it returns 'sara,mike,sara(1),sara(2),Jon' instead of 'sara','mike,'sara(1)','sara(2)','Jon'
Anyone knows how to change that? I tried join, split etc already..