Files
30-seconds-of-code/snippets/countOccurrences.md
2017-12-28 08:30:19 +00:00

340 B

countOccurrences

Counts the occurrences of a value in an array.

Use Array.reduce() to increment a counter each time you encounter the specific value inside the array.

const countOccurrences = (arr, value) => arr.reduce((a, v) => (v === value ? a + 1 : a + 0), 0);
countOccurrences([1, 1, 2, 1, 2, 3], 1); // 3