Counting the occurrences / frequency of array elements

Counting the occurrences / frequency of array elements

In Javascript, I’m trying to take an initial array of number values and count the elements inside it. Ideally, the result would be two new arrays, the first specifying each unique element, and the second containing the number of times each element occurs. However, I’m open to suggestions on the format of the output.
For example, if the initial array was:
5, 5, 5, 2, 2, 2, 2, 2, 9, 4

Then two new arrays would be created. The first would contain the name of each unique element:
5, 2, 9, 4

The second would contain the number of times that element occurred in the initial array:
3, 5, 1, 1

Because the number 5 occurs three times in the initial array, the number 2 occurs five times and 9 and 4 both appear once.
I’ve searched a lot for a solution, but nothing seems to work, and everything I’ve tried myself has wound up being ridiculously complex. Any help would be appreciated!
Thanks đź™‚

Solution 1:

Here you go:

``````function foo(arr) {
var a = [], b = [], prev;

arr.sort();
for ( var i = 0; i < arr.length; i++ ) {
if ( arr[i] !== prev ) {
a.push(arr[i]);
b.push(1);
} else {
b[b.length-1]++;
}
prev = arr[i];
}

return [a, b];
}
``````

Live demo: http://jsfiddle.net/simevidas/bnACW/

Note

This changes the order of the original input array using `Array.sort`

Solution 2:

You can use an object to hold the results:

``````var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
var counts = {};

for (var i = 0; i < arr.length; i++) {
var num = arr[i];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}

console.log(counts[5], counts[2], counts[9], counts[4]);``````

So, now your counts object can tell you what the count is for a particular number:

``````console.log(counts[5]); // logs '3'
``````

If you want to get an array of members, just use the `keys()` functions

``````keys(counts); // returns ["5", "2", "9", "4"]
``````

Solution 3:

``````var a = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4].reduce(function (acc, curr) {
if (typeof acc[curr] == 'undefined') {
acc[curr] = 1;
} else {
acc[curr] += 1;
}

return acc;
}, {});

// a == {2: 5, 4: 1, 5: 3, 9: 1}
``````

Solution 4:

If using underscore or lodash, this is the simplest thing to do:

``````_.countBy(array);
``````

Such that:

``````_.countBy([5, 5, 5, 2, 2, 2, 2, 2, 9, 4])
=> Object {2: 5, 4: 1, 5: 3, 9: 1}
``````

As pointed out by others, you can then execute the `_.keys()` and `_.values()` functions on the result to get just the unique numbers, and their occurrences, respectively. But in my experience, the original object is much easier to deal with.

Solution 5:

Don’t use two arrays for the result, use an object:

``````a      = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
result = { };
for(var i = 0; i < a.length; ++i) {
if(!result[a[i]])
result[a[i]] = 0;
++result[a[i]];
}
``````

Then `result` will look like:

``````{
2: 5,
4: 1,
5: 3,
9: 1
}
``````

Solution 6:

``````const a = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];

const aCount = new Map([...new Set(a)].map(
x => [x, a.filter(y => y === x).length]
));
``````
``````aCount.get(5)  // 3
aCount.get(2)  // 5
aCount.get(9)  // 1
aCount.get(4)  // 1
``````

This example passes the input array to the `Set` constructor creating a collection of unique values. The spread syntax then expands these values into a new array so we can call `map` and translate this into a two-dimensional array of `[value, count]` pairs – i.e. the following structure:

``````Array [
[5, 3],
[2, 5],
[9, 1],
[4, 1]
]
``````

The new array is then passed to the `Map` constructor resulting in an iterable object:

``````Map {
5 => 3,
2 => 5,
9 => 1,
4 => 1
}
``````

The great thing about a `Map` object is that it preserves data-types – that is to say `aCount.get(5)` will return `3` but `aCount.get("5")` will return `undefined`. It also allows for any value / type to act as a key meaning this solution will also work with an array of objects.

``````function frequencies(/* {Array} */ a){
return new Map([...new Set(a)].map(
x => [x, a.filter(y => y === x).length]
));
}

let foo = { value: 'foo' },
bar = { value: 'bar' },
baz = { value: 'baz' };

let aNumbers = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4],
aObjects = [foo, bar, foo, foo, baz, bar];

frequencies(aNumbers).forEach((val, key) => console.log(key + ': ' + val));
frequencies(aObjects).forEach((val, key) => console.log(key.value + ': ' + val));``````