1var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]
2
3const map = arr.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
4
5console.info([...map.keys()]) // to get unique elements
6console.info([...map.values()]) // to get the occurrences
7console.info([...map.entries()]) // to get the pairs [element, frequency]
1Array.prototype.frequencies = function() {
2 var l = this.length, result = {all:[]};
3 while (l--){
4 result[this[l]] = result[this[l]] ? ++result[this[l]] : 1;
5 }
6 // all pairs (label, frequencies) to an array of arrays(2)
7 for (var l in result){
8 if (result.hasOwnProperty(l) && l !== 'all'){
9 result.all.push([ l,result[l] ]);
10 }
11 }
12 return result;
13};
14
15var freqs = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4].frequencies();
16alert(freqs[2]); //=> 5
17// or
18var freqs = '1,1,2,one,one,2,2,22,three,four,five,three,three,five'
19 .split(',')
20 .frequencies();
21alert(freqs.three); //=> 3