AAlgoLoopSpaced repetition for LeetCode
EASYHash Map / SetLeetCode ↗

Unique Number of Occurrences

The key idea

Count how many times each value appears, then ask whether those counts are all different. A set of the counts is unique exactly when its size equals the number of distinct values, so compare len(set(counts)) with len(counts).

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.

In other words, count how many times each distinct value appears. The answer is true when no two distinct values share the same occurrence count.

Constraints

Examples

Input: arr = [1,2,2,1,1,3] Output: true
Input: arr = [1,2] Output: false
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] Output: true

Complexity

Time: O(n) Space: O(n)

See the full solution

410310
Step-by-step visualization
Start free →

More Hash Map / Set problems