The Unique Number of Occurrences problem from LeetCode75 is a beginner-friendly coding interview question that tests your understanding of hash maps, frequency counting, and set operations. It’s often asked in interviews to check whether you can handle frequency analysis and uniqueness logic.

In this blog, we’ll break down the problem, walk through examples and explain a clean Python solution step by step.
Table of Contents
Problem Statement
You are given an integer array arr.
Return true if the number of occurrences of each value in the array is unique otherwise return false.
Understanding the Problem
- Each element in the array appears a certain number of times.
- We need to verify if all frequency counts are different.
- If two numbers have the same frequency return
false.
This is essentially a frequency mapping + uniqueness check problem.
Examples
Example 1:
Input:
arr = [1,2,2,1,1,3]
Output:
True
Explanation:
- Frequency: {1: 3, 2: 2, 3: 1}
- Counts = [3, 2, 1] → All unique → return
True.
Example 2:
Input:
arr = [1,2]
Output:
False
Explanation:
- Frequency: {1: 1, 2: 1}
- Counts = [1, 1] → Not unique → return
False.
Example 3:
Input:
arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output:
True
Explanation:
- Frequency: {-3: 3, 0: 2, 1: 4, 10: 1}
- Counts = [3, 2, 4, 1] → All unique → return
True.
Python Solution – Step by Step
Here’s the clean Python implementation:
from typing import List
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
# Step 1: Count frequencies
count_dict = {}
for num in arr:
count_dict[num] = count_dict.get(num, 0) + 1
# Step 2: Store unique frequency values in a set
unique_counts = set(count_dict.values())
# Step 3: Compare lengths
return len(unique_counts) == len(count_dict)
Step 1: Count Frequencies
count_dict = {}
for num in arr:
count_dict[num] = count_dict.get(num, 0) + 1
We use a dictionary to store each number and its count.
Step 2: Track Unique Occurrences
unique_counts = set(count_dict.values())
Convert frequency values into a set to remove duplicates.
Step 3: Compare Counts
return len(unique_counts) == len(count_dict)
If the number of unique frequencies equals the number of elements in the dictionary, all counts are unique.
Why This Solution Works
- Counting frequencies is straightforward with a hash map.
- Using a set automatically ensures uniqueness.
- A simple length comparison confirms whether counts are unique.
Time and Space Complexity
- Time Complexity:
O(n)→ Counting elements and checking frequencies. - Space Complexity:
O(n)→ Dictionary and set for frequencies.
Edge Cases to Consider
| Input | Output | Explanation |
|---|---|---|
[1,1,2,2,3,3] | False | All numbers appear twice → frequencies not unique. |
[5] | True | Single element always has unique frequency. |
[] | True | Empty array → trivially unique. |
Real-World Applications
- Log analysis: Checking if event frequencies are uniquely distributed.
- Voting systems: Ensuring no two candidates have the exact same number of votes.
- Inventory systems: Identifying unique stock occurrence patterns.
Conclusion
The Unique Number of Occurrences in Python problem may seem straightforward but it is an excellent practice exercise for building strong problem-solving foundations. By combining dictionaries for counting frequencies with sets for validating uniqueness, the solution demonstrates how simple data structures can be leveraged to solve practical coding challenges efficiently. This balance of clarity and performance makes it a favorite for beginners preparing for interviews.
Beyond interview prep, the logic behind this problem connects directly to real-world use cases like detecting anomalies, ensuring data integrity and maintaining uniqueness in systems such as databases or APIs. Mastering this problem equips you with the intuition to handle more advanced tasks in arrays, hashing and frequency analysis, making it both a valuable learning tool and a stepping stone toward more complex algorithmic challenges.
Related Reads
- Find the Difference of Two Arrays in Python – LeetCode Explained
- Find Pivot Index in Python – LeetCode75 Explained
- Transformers, Reasoning & Beyond: A Powerful Deep Dive with Antonio Gulli
- Find the Highest Altitude in Python – LeetCode75 Explained
- Mastering the Longest Subarray of 1’s After Deleting One Element in Python – LeetCode 75 Explained
References
Unique Number of Occurrences – In-Depth Explanation
3 thoughts on “Unique Number of Occurrences in Python – LeetCode75 Explained”