Question Bank

JavaScript Array Occurrences Count: Two Approaches Quiz

Difficulty: Medium

Tally how many times each value appears in an array, two ways (reduce + dict and Map-based counter), plus two companions on Object.create(null) safety and case-insensitive string tallies.

Question Bank
/

JavaScript Array Occurrences Count: Two Approaches Quiz

JavaScript Array Occurrences Count: Two Approaches Quiz

Tally how many times each value appears in an array, two ways (reduce + dict and Map-based counter), plus two companions on Object.create(null) safety and case-insensitive string tallies.

Question Bank
Medium
JavaScript
4 questions
quiz
arrays
hash-map
array-manipulation-patterns

886 views

27

Implement getOccurrences(arr) using Array.prototype.reduce and a plain object accumulator. Return an object keyed by value with the count as the value.

Examples

Example 1:

Input: [1, 12, 8, 5, 7, 1, 3, 5, 12, 7, 11, 8, 12, 5]
Output: { 1: 2, 3: 1, 5: 3, 7: 2, 8: 2, 11: 1, 12: 3 }
Explanation: Each value maps to how many times it appears in the input array.