Explorer
Node.js

Write a function `countOccurrences(arr)` that returns an object with each element as key and its count as value.

Problem Statement

Write a function `countOccurrences(arr)` that returns an object with each element as key and its count as value.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Array.reduce() reduces an array to a single value (which can be an object). šŸ’” Hint 2: Use an object as the accumulator and increment counts. šŸ’” Hint 3: return arr.reduce((acc, item) => { acc[item] = (acc[item] || 0) + 1; return acc; }, {}); āœ… Solution: ```javascript function countOccurrences(arr) { return arr.reduce((acc, item) => { acc[item] = (acc[item] || 0) + 1; return acc; }, {}); } ```

Array.reduce

Easy
Write a function `countOccurrences(arr)` that returns an object with each element as key and its count as value.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"a":3,"b":2,"c":1}