Unique Number of Occurrences
Unique Number of Occurrences: return true if the number of occurrences of every value in the array is unique — that is, no two distinct values appear the same number of times.
- 1 <= arr.length <= 1000
- -1000 <= arr[i] <= 1000
Intuition
Count how often each value appears, then ask whether those counts are all different. Comparing the number of counts against the number of distinct counts answers that in one step: if putting the counts into a set shrinks the collection, two values shared a frequency.
Two-stage counting — count the items, then count the counts — appears whenever a question is about the distribution rather than the data. The len(x) == len(set(x)) idiom is the reusable uniqueness test, and it also settles Contains Duplicate and Determine if Two Strings Are Close.
Approach
Before reading on: after you have the occurrence count of each value, what exactly are you comparing — the values, or the counts? Then find a one-line way to test that a collection has no repeats. Aim for O(n).
Two layers of counting
The question is about frequencies of frequencies, which is what makes it briefly confusing. First build a map from value to its occurrence count — [1,2,2,1,1,3] gives {1: 3, 2: 2, 3: 1}. The values you now care about are 3, 2, 1: the counts themselves. The original numbers have done their job and can be discarded.
Set size is the uniqueness test
A set discards duplicates, so len(set(counts)) is the number of distinct counts while len(counts) is how many there are in total. These are equal exactly when no count repeats. That single comparison replaces any explicit pairwise check, and it runs in O(k) for k distinct values rather than O(k²).
Cost
Building the frequency map is one pass over n elements. Building the set of counts is one pass over the distinct values, at most n of them. So the whole solution is O(n) time and O(n) space — the space is genuinely needed, since you cannot know a frequency until the entire array has been read.
Solution & live demo
Common pitfalls
Testing uniqueness of the values instead of the counts
return len(arr) == len(set(arr))
return len(frequencies) == len(set(frequencies))
That checks whether the elements are distinct, which is a different problem entirely. [1,2,2,1,1,3] has repeated elements yet perfectly unique occurrence counts — the correct answer is true.
Comparing counts pairwise
for a in counts.values():
for b in counts.values():
...return len(frequencies) == len(set(frequencies))
Correct but O(k²), and it needs careful handling to avoid comparing an entry with itself. The set comparison expresses the same idea in one line with no index bookkeeping.
Using the set of the map instead of its values
return len(counts) == len(set(counts))
return len(frequencies) == len(set(frequencies))
Iterating a dict yields its keys, which are distinct by construction — so this always returns true. The counts live in .values().
Edge cases
Every count is 1, so three values share a frequency and the answer is false.
One count of 1, trivially unique, so true.
Both count 2 — the set shrinks from two entries to one and the answer is false.
A hash map keys on any integer, so signs are irrelevant.
A single count of 3 is unique by definition, so true.