Counting the occurrences / frequency of array elements Counting the occurrences / frequency of array elements arrays arrays

Counting the occurrences / frequency of array elements


You can use an object to hold the results:

const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];const counts = {};for (const num of arr) {  counts[num] = counts[num] ? counts[num] + 1 : 1;}console.log(counts[5], counts[2], counts[9], counts[4]);