Unique Number of Occurrences — LeetCode 1207 Python Solution

EasyArrayHash Table
Problem
#1207
Pattern
Hash Map
Reading time
2 min

The problem

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

Example

Input
arr = [1,2,2,1,1,3]
Output
true
Explanation
The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.

Python solution

Python
class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        cnt = Counter(arr)
        return len(set(cnt.values())) == len(cnt)

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 1207. Unique Number of Occurrences is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

On a study list

This problem is on LeetCode 75.

Frequently asked questions

How hard is LeetCode 1207. Unique Number of Occurrences?
LeetCode 1207. Unique Number of Occurrences is rated Easy on LeetCode.
What is the time complexity of LeetCode 1207. Unique Number of Occurrences?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 1207. Unique Number of Occurrences?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 1207. Unique Number of Occurrences cover?
LeetCode 1207. Unique Number of Occurrences is tagged Array and Hash Table on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview