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
Unique Number of Occurrences ProblemCheck if all frequency counts are uniqueStep 1: Input122113Step 2: Count1→32→23→1Step 3: Check UniquenessFrequencies: [3, 2, 1]Set of frequencies: {3, 2, 1}All frequencies are unique!Output: true
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
Asked in
Amazon 15 Microsoft 12
178.0K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen