learnleetcode

1207. Unique Number of Occurrences

Easy · solved 2026-10-01 · Open on LeetCode

hash-tablearray

Time O(n) · Space O(n)

How to get there

Count, then look for a repeated count

1. Solve it by hand. On [1, 2, 2, 1, 1, 3], count each number: 1 appears 3 times, 2 twice, 3 once. Then check the counts 3, 2, 1: no two are equal, so the answer is true.

2. Notice the two layers. The first layer asks how often each number appears. The second asks whether any two numbers share a count. Both are the same question underneath: have I seen this before? The first asks it of numbers, the second of counts.

3. Pick a hashmap for each layer. A map from number to count answers the first layer in one pass. A second map, keyed by count, answers the second: each count is claimed by the first number that has it, and a count already claimed means two numbers share it.

4. Stop at the first clash. One shared count settles the answer, so the scan returns False there without checking the rest.

The reusable recipe: when a question is about a property of the counts, count first, then treat the counts as the new data and ask the question of them.

Let the variables' meanings write the branches

Variable Always means
counter maps each number read so far to how often it has appeared
unique maps each count checked so far to the number that claimed it first
Code What happened What it restores
if num not in counter: counter[num] = 1 a number appears for the first time it is counted once
else: counter[num] += 1 a number appears again its count goes up by one
if counter[num] not in unique: unique[counter[num]] = num a count no number has claimed yet this number claims it
elif unique[counter[num]] != num: return False a count already claimed another number has the same count, so the answer is false
return True every count was claimed once no two numbers share a count

Each number comes up once as a key of counter, so a claimed count was always claimed by a different number: the elif test is always true when it runs.

The method carries over: give each map one meaning, and each branch is either a first sighting or a repeat of what that map tracks.

Alternatives compared

Approach Time Space How it works
Sort and count runs O(n log n) O(n) Sorted, equal numbers sit together; each run’s length is a count, collected and checked for repeats.
Two hashmaps (chosen) O(n) O(n) Count each number, then claim each count in a second map; a count already claimed returns False.
Set of counts O(n) O(n) len(set(counter.values())) == len(counter): the counts are unique exactly when no count collapses in the set.
Count arrays O(n + R) O(R) The numbers lie in −1000…1000, so a list of R = 2001 slots can hold the counts in place of a hashmap.

The two hashmaps stop at the first shared count. The set of counts is shorter but always builds the whole set before it compares.

solution.py

class Solution:
    def uniqueOccurrences(self, arr: list[int]) -> bool:
        unique, counter = {} , {}

        for num in arr:
            if num not in counter:
                counter[num] = 1
            else:
                counter[num] += 1

        for num in counter:
            if counter[num] not in unique:
                unique[counter[num]] = num
            elif unique[counter[num]] != num:
                return False

        return True

Notes

Write your approach, insights, and mistakes here. Any other file in this folder (e.g. solution.py, attempt-1.py) is rendered below as code.