Unique Number of Occurrences - 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: No two different values should have the same frequency count.
Input & Output
Example 1 — All Unique Frequencies
$
Input:
arr = [1,2,2,1,1,3]
›
Output:
true
💡 Note:
Element 1 appears 3 times, element 2 appears 2 times, element 3 appears 1 time. Frequencies [3,2,1] are all unique.
Example 2 — Duplicate Frequencies
$
Input:
arr = [1,2]
›
Output:
false
💡 Note:
Element 1 appears 1 time, element 2 appears 1 time. Both have frequency 1, so frequencies [1,1] are not unique.
Example 3 — Single Element
$
Input:
arr = [5]
›
Output:
true
💡 Note:
Only one element with frequency 1. Since there's only one frequency count, it's trivially unique.
Constraints
- 1 ≤ arr.length ≤ 1000
- -1000 ≤ arr[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array of integers with repeated elements
2
Count Frequencies
Use hash map to count each element's occurrences
3
Check Uniqueness
Verify all frequency counts are different
Key Takeaway
🎯 Key Insight: Use a hash map to count frequencies, then check if all counts are unique using a set
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code