Fundamentals/Hashmap Intro
← PrevNext →
Given an integer array, return a frequency hash map (object) that maps each unique value in the array to the number of times it appears.
The keys of the returned map are the distinct values found in the array, and each associated value is the count of occurrences. If the input array is empty, return an empty map.
Input
arr: an array of integers
Output
An object whose keys are the unique array values (as numeric keys) and whose values are the integer counts of how many times each key appears in arr.
Examples
Example 1
Inputarr = [1, 2, 1, 3, 2, 1]
Output{"1": 3, "2": 2, "3": 1}
Explanation: 1 appears three times, 2 appears twice, and 3 appears once.
Example 2
Inputarr = [7, 7, 7, 7]
Output{"7": 4}
Explanation: The single distinct value 7 occurs four times.
Example 3
Inputarr = []
Output{}
Constraints
  • 0 <= arr.length
  • Each element of arr is an integer.
  • Each insertion and lookup should be O(1) on average.