Answers for "javascript Count the occurrences of a value in an array"

8

javascript Count the occurrences of a value in an array

const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);

// Examples
countOccurrences([2, 1, 3, 3, 2, 3], 2);                // 2
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b'], 'a');  // 3
Posted by: Guest on July-02-2020

Code answers related to "javascript Count the occurrences of a value in an array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language