javascript count frequency in array code example
Example 1: javascript Count the frequency of a value in an array
const countOccurrences = arr => arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {});
countOccurrences([2, 1, 3, 3, 2, 3]);
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b']);
Example 2: Count frequency of array elements js
var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]
const map = arr.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
console.info([...map.keys()])
console.info([...map.values()])
console.info([...map.entries()])