Javascript: Count duplicates in an array

Problem:

You have a javascript array that likely has some duplicate values and you would like a count of those values.

Solution:

Try this schnippet out.
 
function compressArray(original) {
 
	var compressed = [];
	// make a copy of the input array
	var copy = original.slice(0);
 
	// first loop goes over every element
	for (var i = 0; i < original.length; i++) {
 
		var myCount = 0;	
		// loop over every element in the copy and see if it's the same
		for (var w = 0; w < copy.length; w++) {
			if (original[i] == copy[w]) {
				// increase amount of times duplicate is found
				myCount++;
				// sets item to undefined
				delete copy[w];
			}
		}
 
		if (myCount > 0) {
			var a = new Object();
			a.value = original[i];
			a.count = myCount;
			compressed.push(a);
		}
	}
 
	return compressed;
};

It should go something like this:

var testArray = new Array("dog", "dog", "cat", "buffalo", "wolf", "cat", "tiger", "cat");
var newArray = compressArray(testArray);
 
/*
console: [
	Object { value="dog", count=2}, 
	Object { value="cat", count=3}, 
	Object { value="buffalo", count=1}, 
	Object { value="wolf", count=1}, 
	Object { value="tiger", count=1}
]
*/

I hope that’s useful to somebody.


11 Comments

  1. The above example worked AWESOME…………………………….

    THANK U…….

    Comment by mvinay — January 16, 2012 @ 5:58 am

  2. Thank you so much! Great piece of code, you are a legend!

    Comment by Mark — February 3, 2012 @ 9:35 pm

  3. Awesome , just what i needed

    Comment by Danyal — March 6, 2012 @ 2:21 am

  4. Thanks a lot this helped me to finish my problem that i have been thinking how to do. I modified your code just so that it would return array insted of Object and its working great.

    Comment by Eddi — May 10, 2012 @ 8:51 am

  5. I precisely wanted to say thanks again. I am not sure the things that I might have gone through in the absence of the recommendations documented by you relating to this area. Entirely was a real alarming difficulty for me personally, however , spending time with this professional strategy you processed the issue made me to cry over contentment. I will be thankful for the guidance and then pray you really know what a powerful job you are putting in teaching others via a web site. Probably you’ve never come across all of us.

    Comment by how to jailbreak iphone — May 23, 2012 @ 3:48 am

  6. Thanks a lot for a great function and tutorial. This function is exactly what i am looking for.

    Comment by Dave — May 30, 2012 @ 8:29 pm

  7. Exactly what I was looking for. Thanks.

    Comment by Vlad — July 17, 2012 @ 10:03 pm

  8. Yes, useful, thanks.

    Comment by ken — September 24, 2012 @ 2:55 am

  9. Hello! beekeda interesting beekeda site! I’m really like it! Very, very beekeda good!

    Comment by Pharmd767 — October 26, 2012 @ 8:16 pm

  10. Thanks, this is perfect and exactly what i need now.

    Comment by Daniel — December 5, 2012 @ 9:40 pm

  11. very nice works like magic

    Comment by Yene — February 1, 2013 @ 8:42 am

Sorry, the comment form is closed at this time.