LeetCode #1207 Easy

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.

Constraints
  • 1 <= arr.length <= 1000
  • -1000 <= arr[i] <= 1000
arrayhash-table
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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).

1

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.

2

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²).

3

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.

04

Solution & live demo

1class Solution:
2 def uniqueOccurrences(self, arr):
3 counts = Counter(arr)
4 frequencies = list(counts.values())
5 return len(frequencies) == len(set(frequencies))
05

Common pitfalls

Testing uniqueness of the values instead of the counts

✗ Wrong
return len(arr) == len(set(arr))
✓ Right
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

✗ Wrong
for a in counts.values():
    for b in counts.values():
        ...
✓ Right
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

✗ Wrong
return len(counts) == len(set(counts))
✓ Right
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().

06

Edge cases

All values distinct, e.g. [1,2,3]

Every count is 1, so three values share a frequency and the answer is false.

Single element, e.g. [5]

One count of 1, trivially unique, so true.

Two values with equal counts, e.g. [1,1,2,2]

Both count 2 — the set shrinks from two entries to one and the answer is false.

Negative numbers

A hash map keys on any integer, so signs are irrelevant.

All identical, e.g. [3,3,3]

A single count of 3 is unique by definition, so true.

07

Complexity

Time
O(n)
Space
O(n)
One pass to count, one over the distinct values. The map holds at most n entries.