Unique Number of Occurrences - Problem
You are given an array of integers arr. Your task is to determine if the frequency count of each unique value in the array is itself unique across all values.
In other words, no two different numbers should appear the same number of times in the array.
Example: If the number 3 appears twice and the number 5 also appears twice, then the answer is false because both numbers have the same frequency (2).
Return true if all frequency counts are unique, or false otherwise.
Input & Output
example_1.py โ Basic Case
$
Input:
[1,2,2,1,1,3]
โบ
Output:
true
๐ก Note:
The value 1 appears 3 times, 2 appears 2 times, and 3 appears 1 time. All frequencies (3, 2, 1) are unique.
example_2.py โ Duplicate Frequencies
$
Input:
[1,2]
โบ
Output:
false
๐ก Note:
Both 1 and 2 appear exactly 1 time each. Since both have the same frequency (1), the frequencies are not unique.
example_3.py โ All Same Elements
$
Input:
[1,1,1,1]
โบ
Output:
true
๐ก Note:
Only one unique value (1) appears 4 times. Since there's only one frequency count, it's trivially unique.
Visualization
Tap to expand
Understanding the Visualization
1
Count Checkouts
Count how many times each book was checked out: Book A (3 times), Book B (2 times), Book C (1 time)
2
Check for Duplicates
Verify that no two books have the same checkout count
3
Determine Result
If all checkout counts are different, return true; otherwise false
Key Takeaway
๐ฏ Key Insight: We only care about frequency counts being unique, not the actual values or their positions in the array
Time & Space Complexity
Time Complexity
O(n)
Single pass to build frequency map plus O(k) to check uniqueness where k โค n
โ Linear Growth
Space Complexity
O(n)
Hash map for frequencies plus set for uniqueness check
โก Linearithmic Space
Constraints
- 1 โค arr.length โค 1000
- -1000 โค arr[i] โค 1000
- All elements are integers
- Array can contain negative numbers and duplicates
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code