Unique Number of Occurrences

Easy Hash Maps

Problem

Given an array of integers arr, return true if the number of occurrences of each value in the array is unique, and false otherwise.

  • 1 ≤ arr.length ≤ 1000
  • −1000 ≤ arr[i] ≤ 1000

Example

Input: arr = [1,2,2,1,1,3]
Output: true

Counting the occurrences yields {1:3, 2:2, 3:1}. The frequencies are [3, 2, 1], which are all unique. Therefore, the function returns true.

Approach

Straightforward Solution

A naive approach would count frequencies and then compare each frequency against all others to detect duplicates, resulting in O(n²) time complexity, which is inefficient for larger inputs.

Core Observation

The problem reduces to verifying that the frequency counts of each distinct element are all unique. This means no two elements share the same count.

Path to Optimal

The key insight is to use a hash map (or Counter) to count frequencies in O(n) time, then convert the frequency values into a set to detect duplicates efficiently. Since sets only store unique elements, comparing the size of the frequency list and the set reveals if any duplicates exist.

Optimal Approach

Use Python's Counter to count occurrences, extract the frequency values into a list, and compare the length of this list to the length of the set of frequencies. If equal, all frequencies are unique; otherwise, duplicates exist. This approach runs in O(n) time with O(n) space.

Time

O(n)

Counting frequencies with Counter takes O(n) time, and creating a set from the frequency values also takes O(n) time. The overall complexity is linear in the size of the input array.

Space

O(n)

The Counter stores frequencies for up to n distinct elements, and the set stores up to n frequency values. Both scale linearly with input size, which is necessary to track counts.

Pattern Spotlight

Hash Maps (Frequency Counting and Uniqueness Check)

When verifying uniqueness of counts or values derived from elements, transform the problem into frequency counting with a hash map, then use a set to detect duplicates efficiently by comparing sizes.

Solution

1from collections import Counter
2
3class Solution:
4 def uniqueOccurrences(self, arr: list[int]) -> bool:
5 count = Counter(arr)
6 frequencies = list(count.values())
7
8 return len(frequencies) == len(set(frequencies))

Step-by-Step Solution

1

Count Element Frequencies Using Counter

5count = Counter(arr)

Objective

To efficiently count how many times each distinct integer appears in the input array.

Key Insight

Using a hash map-like data structure such as Counter allows counting frequencies in a single pass with O(n) time. This transforms the problem from dealing with raw elements to dealing with their occurrence counts, which is the core data needed to check uniqueness.

Interview Quick-Check

Core Logic

Counter builds a frequency map in O(n) time by iterating once over the input array.

Common Pitfalls & Bugs

Manually counting frequencies with nested loops leads to O(n²) time, which is inefficient.

2

Extract Frequencies and Compare Uniqueness via Set

6frequencies = list(count.values())
8return len(frequencies) == len(set(frequencies))

Objective

To determine if all frequency counts are unique by comparing the list of frequencies to a set of those frequencies.

Key Insight

Converting the frequency values to a set removes duplicates. If the size of the set equals the size of the frequency list, it guarantees all frequencies are unique. This simple comparison leverages the properties of sets to efficiently detect duplicates without explicit pairwise checks.

Interview Quick-Check

Core Logic

Comparing lengths of the frequency list and its set counterpart is a constant-time uniqueness check after O(n) frequency extraction.

Common Pitfalls & Bugs

Forgetting to convert frequencies to a list before comparing lengths or misusing sets can lead to incorrect results.

Line Analysis

This solution has 2 Critical lines interviewers watch for.

Line 8 Critical
return len(frequencies) == len(set(frequencies))

Return whether all frequency counts are unique by comparing the length of the frequency list to the length of the set of frequencies.

Sets automatically remove duplicates, so if the lengths differ, it means some frequencies are repeated; if equal, all frequencies are unique. This comparison is a concise and efficient way to verify uniqueness.

Line 5 Critical
count = Counter(arr)

Count the occurrences of each integer in the input array using Counter.

Counter efficiently builds a frequency map in O(n) time by hashing each element and incrementing its count, enabling fast frequency retrieval for all distinct elements.

Line 6 Core
frequencies = list(count.values())

Extract the list of frequency counts from the Counter object.

This isolates the frequency values, which are the key data needed to check for uniqueness, separating them from the keys (elements) themselves.

Test Your Understanding

Why does comparing the length of the frequency list to the length of the set of frequencies determine uniqueness?

Because a set only stores unique elements, if the lengths differ, it means there are duplicate frequency values; if they are equal, all frequencies are unique.

Related Problems

Hash Maps pattern

Don't just read it. Drill it.

Reconstruct Unique Number of Occurrences from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.

Unlock the Unique Number of Occurrences drill

or drill a free problem