Unique Number of Occurrences
The key idea
Count how many times each value appears, then ask whether those counts are all different. A set of the counts is unique exactly when its size equals the number of distinct values, so compare len(set(counts)) with len(counts).
Problem
Given an array of integers arr, return true if the number of occurrences of each value in the array is unique, or false otherwise.
In other words, count how many times each distinct value appears. The answer is true when no two distinct values share the same occurrence count.
Constraints
- 1 <= arr.length <= 1000
- -1000 <= arr[i] <= 1000
Examples
Input: arr = [1,2,2,1,1,3]
Output: true
Input: arr = [1,2]
Output: false
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output: true
Complexity
Time: O(n) Space: O(n)
See the full solution
- ✓Full worked approach
- ✓Reference code in 5 languages
- ✓Problem-solving tips
- ✓Step-by-step animated visualization
More Hash Map / Set problems
- 4Sum IIMEDIUM
- Contains DuplicateEASY
- Contains Duplicate IIEASY
- Determine if Two Strings Are CloseMEDIUM
- First Missing PositiveHARD
- Equal Row and Column PairsMEDIUM
- Find the Difference of Two ArraysEASY
- Group AnagramsMEDIUM
- Intersection of Two ArraysEASY
- Isomorphic StringsEASY
- Longest Consecutive SequenceMEDIUM
- Longest PalindromeEASY