3

I am trying to append the count to duplicate entries in a string array. I have an array like this which contains duplicate entries.

var myarray = ["John", "John", "John", "Doe", "Doe", "Smith", 
               "John", "Doe", "Joe"];

My desired output is

var newArray = ["John - 1", "John - 2", "John - 3", "Doe - 1", 
                "Doe - 2", "Smith", "John - 4", "Doe - 3", "Joe"];

What is the best way to do this?

3 Answers 3

8

This works, using two passes of Array.map():

var map = {};
var count = myarray.map(function(val) {
    return map[val] = (typeof map[val] === "undefined") ? 1 : map[val] + 1;
});

var newArray = myarray.map(function(val, index) {
    if (map[val] === 1) {
        return val;
    } else {
        return val + ' - ' + count[index];
    }
});

The first pass records the number of times each unique item has been seen, and returns an array corresponding to the input array recording the current count for each item.

The second pass appends the count to any items whose total count was not one.

Working demo at http://jsfiddle.net/alnitak/Z4dgr/

Sign up to request clarification or add additional context in comments.

1 Comment

Elegant, but notice that he wants only duplicate entries altered. So you probably have to make two passes.
4

something like

var counts = {}, newArray = [];

// first pass, assign the counts
for (var i = 0; i < myArray.length; i++) {
   var name = myArray[i],
       count = counts.name;

   if (!count) {
      count = 1;
   } else {
      count++;
   }

   counts[name] = count;

   name = name + " - " + count;
   newArray.push(name);
}

// Undo changes made for names that only occur once
for (var i = 0; i < myArray.length; i++) {
   var name = myArray[i],
       count = counts.name;

   if (count === 1) {
      newArray[i] = name;
   }

}

note I'm using very simple javascript to keep the solution easily readable, understandable. Note you have to pass over the array twice. For very large lists, this is not very efficient.

I didn't test this, but something like that should work.

Comments

0

This should tell you the count of all duplicates:

var myarray = ["John", "John", "John", "Doe", "Doe", "Smith", 
           "John", "Doe", "Joe"];
var values = {}, newArray = [];
for (var i = 0; i < myarray.length; i++) {
    if (typeof values[myarray[i]] === 'undefined') {
        values[myarray[i]] = 1;
    } else {
        values[myarray[i]] += 1;
    }
}
for (var i = 0; i < myarray.length; i++) {
    if (values[myarray[i]] > 1) {
        newArray.push(myarray[i] + ' - ' + values[myarray[i]]);
    } else {
        newArray.push(myarray[i]);
    }
}

Best regards!

2 Comments

he wants to append the running count, not the total count.
Yes, haven't noticed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.