Files
30-seconds-of-code/snippets/countOccurrences.md
2017-12-17 17:55:51 +02:00

323 B

countOccurrences

Counts the occurences 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