How to count array elements by each element in javascript [duplicate]

var counts = {};

for (var i = 0; i < array.length; i++)
    counts[array[i]] = (counts[array[i]] + 1) || 1;


console.log(counts);

This assumes a toString representation of the items in the Array will be acceptable. For example, it will see 1 as being the same as "1".

Given your example Array, this will not be an issue.

Leave a Comment