File: unique-number-of-occurrences/solution.py

Date: 2026-06-06

Time: 19:36

unique-number-of-occurrences/solution.py

Purpose

This file solves LeetCode 1207 — Unique Number of Occurrences. It determines whether every distinct value in an integer array appears a unique number of times. For example, [1,2,2,1,1,3] returns True because 1 appears 3 times, 2 appears 2 times, and 3 appears 1 time — all distinct counts.

Key Components

Solution.uniqueOccurrences(self, arr: List[int]) -> bool — The single method. Takes a list of integers, returns a boolean. The contract is: return True if and only if no two distinct values share the same frequency.

Patterns

The solution follows a count-then-deduplicate idiom that's common across this repo's frequency-based problems:

1. CountCounter(arr) builds a frequency map in O(n).

2. Deduplicate — Convert the counts to a set and compare cardinalities. If any two values had the same count, the set will be smaller than the original collection.

This is the canonical two-liner for "are all X unique" checks: len(xs) == len(set(xs)).

Dependencies

Imports:

Imported by: The testsolution.py in this directory plus hundreds of other test files across the repo. The "Imported By" list in the prompt is misleading — those other test files don't actually import *this* solution; they import their own local solution.py. The only real consumer is unique-number-of-occurrences/testsolution.py.

Flow


arr → Counter(arr) → .values() → compare len(values) vs len(set(values)) → bool

No loops, no branching, no mutation. It's a pure functional pipeline compressed into two expressions.

Invariants

Error Handling

None. The method trusts the caller to pass a valid List[int] per the LeetCode contract. Empty input would return True (0 == 0), which is a reasonable default even though the constraint forbids it.

Topics to Explore

Beliefs