Skip to content
← All posts

Unique Number of Occurrences: Hash Map Frequency Counting

4 min read
leetcodeproblemeasyarrayshash-map

LeetCode 1207 Unique Number of Occurrences asks a deceptively simple question: given an array of integers arr, return True if the number of occurrences of each value in the array is unique, or False otherwise.

For example, arr = [1, 2, 2, 1, 1, 3] has three 1s, two 2s, and one 3. The counts are 3, 2, and 1. No two values share the same count, so you return True. But arr = [1, 2] has one 1 and one 2. Both values appear once, so the counts are not unique, and you return False.

arr102122131435count frequenciesvaluecount132231counts: 3, 2, 1all unique!return True
arr = [1, 2, 2, 1, 1, 3]. Frequency counts are 3, 2, and 1. Since no two values share the same count, return True.

Why this problem matters

This is a textbook frequency counting problem, and it combines two of the most fundamental data structures in algorithm interviews: the hash map and the set. You use the hash map to count, and the set to check uniqueness. These two tools show up together in dozens of problems. Getting comfortable with this pairing here, where the logic is minimal, means you will recognize it instantly when the surrounding problem is harder.

The key insight

Count the frequency of each value using a Counter (or dictionary), then check whether all the frequency values are unique. The fastest way to check uniqueness? Put them in a set. If the set has the same size as the number of entries, every count was distinct. If the set is smaller, at least two values had the same count.

That is the entire algorithm: one pass to count, one comparison to decide.

The solution

from collections import Counter

def unique_occurrences(arr: list[int]) -> bool:
    counts = Counter(arr)
    return len(counts) == len(set(counts.values()))

You build a Counter from the array, which gives you a dictionary mapping each value to its frequency. Then you extract just the frequency values with counts.values(), wrap them in a set to remove duplicates, and compare sizes. If the lengths match, every frequency was unique.

The len(counts) == len(set(counts.values())) comparison is a general trick for checking if all values in a collection are distinct. You will use this same pattern in other problems where you need to verify uniqueness of any computed property.

Visual walkthrough

Step 1: Start with the input array

arr122113

We need to determine if each value has a unique number of occurrences.

Step 2: Count frequencies with a hash map

arr122113valuecount132231

Walk through the array once. 1 appears 3 times, 2 appears 2 times, 3 appears 1 time.

Step 3: Collect counts into a set and check uniqueness

arr122113valuecount132231count set{3, 2, 1}

Put all frequency values into a set: {3, 2, 1}. If a count were duplicated, the set would be smaller than the number of entries.

Step 4: Compare sizes and return the result

arr122113valuecount132231count set{3, 2, 1}3 counts = 3 unique

3 frequency values and 3 unique values in the set. They match, so every count is unique. Return True.

The walkthrough shows the two-phase approach clearly. First you scan the entire array to build the frequency map. Then you check the frequency values for uniqueness using a set. No sorting, no nested loops. Just two linear passes over the data.

Complexity analysis

ApproachTimeSpace
Hash map + setO(n)O(n)

Time: O(n). You iterate through the array once to build the Counter. Extracting values and building the set are both O(k) where k is the number of distinct values, and k is at most n. Total: O(n).

Space: O(n). The Counter stores up to n entries (if every element is unique). The set of frequency values stores up to k entries. Both are bounded by n.

The building blocks

1. Frequency counting with Counter

from collections import Counter

counts = Counter(arr)

This single line does what would otherwise take a loop and a defaultdict. Counter walks through the iterable and builds a dictionary of {element: count} pairs. It is the go-to tool any time a problem asks "how many times does each value appear?"

2. Uniqueness check with set

len(collection) == len(set(collection))

This is the standard Python idiom for checking whether all elements in a collection are distinct. A set removes duplicates, so if the size drops, something was repeated. You will use this pattern for checking unique row values, unique hash keys, unique distances, and more.

Edge cases

  • All elements identical. arr = [3, 3, 3] has one distinct value with count 3. One count, one entry in the set. Return True.
  • All elements distinct. arr = [1, 2, 3] has three values each with count 1. Three counts but only one unique count. Return False.
  • Single element. arr = [5] has one value with count 1. Return True.
  • Two values with the same count. arr = [1, 2] has counts [1, 1]. The set is , which is smaller than the count list. Return False.
  • Negative numbers. Counters handle negatives the same as positives. No special handling needed.

From understanding to recall

The solution to Unique Number of Occurrences is short enough to memorize, but that is not the point. The real skill is recognizing the "count then check uniqueness" pattern when it appears in a harder problem. Maybe you need to verify that all rows in a matrix have unique sums, or that all nodes in a tree have unique depths. The shape is always the same: build a frequency map, extract the values, check if they are unique.

Spaced repetition drills this at the building-block level. You practice the Counter pass and the set uniqueness check as separate micro-patterns, typing them from scratch at increasing intervals. After a few cycles, reaching for Counter and len(x) == len(set(x)) becomes automatic. You spend your interview time on the hard parts of the problem, not reinventing the frequency counting step.

Related posts

CodeBricks breaks problems like Unique Number of Occurrences into reusable building blocks and drills them with spaced repetition. Instead of re-solving the same problem from scratch each time, you practice the underlying patterns until they are automatic.