EasyNo. 1207

Unique Number of Occurrences — LeetCode 1207 Python Solution

Pattern
Hash Map
Topics
Array, Hash Table
On a study list
LeetCode 75
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

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

Complexity

Time
O(n)
Space · auxiliary
O(n)

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

Related problems
No.ProblemDifficulty
1202Smallest String With SwapsMedium
0001Two SumEasy
0036Valid SudokuMedium
0037Sudoku SolverHard
0041First Missing PositiveHard
0049Group AnagramsMedium

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.

Free to download. Check it runs on your machine, then pick a plan.