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
Book A๐Ÿ“š๐Ÿ“š๐Ÿ“š3 timesBook B๐Ÿ“š๐Ÿ“š2 timesBook C๐Ÿ“š1 timeCheckout Count Analysis:321All checkout counts are different!โœ“ UNIQUE OCCURRENCES
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

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map for frequencies plus set for uniqueness check

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 1000
  • -1000 โ‰ค arr[i] โ‰ค 1000
  • All elements are integers
  • Array can contain negative numbers and duplicates
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
89.5K 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