Problem Walkthrough

Unique Occurrences LeetCode 1207 Guide

Count the frequency of each value using a hash map, then verify all frequencies are unique by comparing the count map size against a set of its values — a clean two-step approach.

6 min read|

Unique Occurrences

LeetCode 1207

Problem Overview

LeetCode 1207 gives you an integer array arr and asks you to return true if the number of occurrences of each value in the array is unique — meaning no two distinct values share the same frequency count. If any two values appear the same number of times, return false.

For example, arr=[1,2,2,1,1,3] has value 1 appearing 3 times, value 2 appearing 2 times, and value 3 appearing 1 time. The frequency set is {3, 2, 1} — all distinct — so the answer is true. Contrast with arr=[1,2] where both values appear once: frequencies {1, 1} are not all unique, so the answer is false.

The problem is straightforward once you identify the two-step structure: first build a frequency map, then verify the frequencies themselves are all distinct. The constraints are small enough that any reasonable approach works well.

  • 1 <= arr.length <= 1000 — small input, any O(n log n) or O(n) solution is fine
  • -1000 <= arr[i] <= 1000 — values can be negative, so use a hash map not an array index
  • Return true if every value has a unique occurrence count, false otherwise
  • Occurrence counts themselves must all be distinct — no two values share the same count
  • A single-element array always returns true — one value, one frequency, trivially unique

Two-Step Approach

The solution breaks into two independent steps. Step 1: count the frequency of every value in the array using a hash map or Counter. After this pass, you have a dictionary mapping each distinct value to the number of times it appears.

Step 2: check whether all the frequency counts are unique. Extract the values (counts) from the frequency map and place them in a set. If the set size equals the number of distinct values (the map size), every frequency was unique — return true. If the set is smaller, some counts were duplicated — return false.

This two-step decomposition is clean and general: any time you need to verify a property about frequencies rather than values themselves, build the frequency map first, then apply the property check to the map's values.

💡

Python Two-Liner

This is a two-liner in Python: counts = Counter(arr) for frequencies, then return len(counts) == len(set(counts.values())) to check uniqueness. If the set of frequency values has the same size as the map, all frequencies are distinct.

Why Counter + Set Works

Counter(arr) builds a dictionary mapping each unique value to its occurrence count. For arr=[1,2,2,1,1,3], Counter produces {1:3, 2:2, 3:1}. The map has 3 keys — one for each distinct value in the array.

Taking set(counts.values()) converts the collection of frequency numbers [3, 2, 1] into a set {3, 2, 1}. A set discards duplicates, so if any two frequencies were equal the set would be smaller than the original list. Comparing len(counts) == len(set(counts.values())) captures this exactly: equal lengths mean no duplicates were discarded.

This works because Counter guarantees every key maps to a positive integer count, so the values list is non-empty and well-defined. The set comparison is O(k) where k is the number of distinct values — typically much smaller than n.

  1. 1Build counts = Counter(arr) — maps each value to its frequency
  2. 2Extract freq_list = list(counts.values()) — the list of all frequency counts
  3. 3Build freq_set = set(freq_list) — duplicates are automatically removed
  4. 4Compare len(freq_list) == len(freq_set) — equal means all frequencies unique
  5. 5Return the boolean result of that comparison

Alternative: Sorting

Instead of a set, you can sort the frequency values and scan for adjacent duplicates. After building the frequency map, extract the values into a list, sort it, and iterate through adjacent pairs. If any pair is equal, the frequencies are not all unique — return false. If you reach the end without finding a duplicate, return true.

This approach is O(n log n) in the worst case due to sorting, compared to O(n) for the set approach. In practice, k (the number of distinct values) is bounded by min(n, 2001) given the constraints, so sorting is at most O(2001 log 2001) — effectively constant. Both approaches are perfectly valid for this problem size.

The sorting approach may be slightly more intuitive if you think of the problem as "are there repeated elements in this list?" — a classic duplicate-detection pattern. The set approach is more idiomatic in Python and Java and is the preferred one-liner solution.

ℹ️

Complexity Comparison

The set approach is O(n) total: O(n) for counting + O(k) for set construction where k = number of unique values. Sorting the frequency list is O(k log k) which is also fine since k <= n — and k is bounded by the value range, so both run in essentially linear time for this problem's constraints.

Walk-Through Example

Example 1: arr=[1,2,2,1,1,3]. Count frequencies: Counter gives {1:3, 2:2, 3:1}. Frequency values list: [3, 2, 1]. Set of frequencies: {3, 2, 1}. len([3,2,1]) = 3 == len({3,2,1}) = 3 → true. All three values have distinct occurrence counts.

Example 2: arr=[1,2]. Count frequencies: Counter gives {1:1, 2:1}. Frequency values list: [1, 1]. Set of frequencies: {1}. len([1,1]) = 2 != len({1}) = 1 → false. Both values appear exactly once, so the occurrence counts are not unique.

Example 3: arr=[3,3,3,5,5] → Counter gives {3:3, 5:2} → values [3, 2] → set {3, 2} → 2 == 2 → true. Now add a 5: arr=[3,3,3,5,5,5] → {3:3, 5:3} → values [3,3] → set {3} → 2 != 1 → false.

  1. 1arr=[1,2,2,1,1,3]: freq = {1:3, 2:2, 3:1}, values=[3,2,1], set={3,2,1}, 3==3 → true
  2. 2arr=[1,2]: freq = {1:1, 2:1}, values=[1,1], set={1}, 2!=1 → false
  3. 3arr=[3,3,3,5,5]: freq = {3:3, 5:2}, values=[3,2], set={3,2}, 2==2 → true
  4. 4arr=[3,3,3,5,5,5]: freq = {3:3, 5:3}, values=[3,3], set={3}, 2!=1 → false
  5. 5arr=[1]: freq = {1:1}, values=[1], set={1}, 1==1 → true (single element trivially true)

Code Walkthrough: Python and Java

Python one-liner: from collections import Counter; return len(counts := Counter(arr)) == len(set(counts.values())). The walrus operator assigns Counter(arr) to counts inline, then compares map size to set-of-values size. A slightly more readable version: counts = Counter(arr); return len(counts) == len(set(counts.values())).

Java solution: use a HashMap<Integer, Integer> to count frequencies. Iterate arr and call map.merge(num, 1, Integer::sum) for each element. Then collect values into a HashSet<Integer> and compare sizes: return map.size() == new HashSet<>(map.values()).size(). Both the HashMap and HashSet operations are O(n) time and O(n) space.

Both Python and Java solutions run in O(n) time and O(n) space. The dominant cost is the linear scan to build the frequency map. Set construction from k values is O(k) and k <= n, so the overall complexity is O(n). Space is O(k) for the map and O(k) for the set.

⚠️

Values vs Frequencies Confusion

Don't confuse "unique values" with "unique frequencies": arr=[1,1,2,2] has unique values {1,2} but non-unique frequencies {2,2} → false. The problem asks whether the counts are unique, not whether the values are unique — the values in arr are always the keys, and you are checking the map's values (counts).

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now