Files
30-seconds-of-code/snippets/countOccurrences.md
2017-12-27 16:21:57 +02:00

332 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