Program to check occurrences of every value is unique or not in Python

Suppose we have a list of numbers nums (positive or negative), we have to check whether the number of occurrences of every value in the array is unique or not.

So, if the input is like nums = [6, 4, 2, 9, 4, 2, 2, 9, 9, 9], then the output will be True, as there is 1 occurrence of 6, 2 occurrences of 4, 3 occurrences of 2, and 4 occurrences of 9. So all number of occurrences are unique.

Approach

To solve this, we will follow these steps ?

  • Count the occurrences of each number in the list

  • Get all occurrence counts as a list

  • Check if all occurrence counts are unique by comparing the length of the counts list with the length of its set

Using Counter from collections

The Counter class provides an easy way to count occurrences of elements ?

from collections import Counter

class Solution:
    def solve(self, nums):
        num_counts = dict(Counter(nums))
        occurrences = list(num_counts.values())
        return len(occurrences) == len(set(occurrences))

# Test the solution
ob = Solution()
nums = [6, 4, 2, 9, 4, 2, 2, 9, 9, 9]
result = ob.solve(nums)
print(f"Input: {nums}")
print(f"Output: {result}")
Input: [6, 4, 2, 9, 4, 2, 2, 9, 9, 9]
Output: True

Using Dictionary Count

We can also solve this without using Counter by manually counting occurrences ?

def unique_occurrences(nums):
    # Count occurrences manually
    count_dict = {}
    for num in nums:
        count_dict[num] = count_dict.get(num, 0) + 1
    
    # Get all occurrence counts
    occurrences = list(count_dict.values())
    
    # Check if all counts are unique
    return len(occurrences) == len(set(occurrences))

# Test with different examples
test_cases = [
    [6, 4, 2, 9, 4, 2, 2, 9, 9, 9],  # True: 1,2,3,4 occurrences
    [1, 2, 2, 1, 1, 3],              # True: 3,2,1 occurrences  
    [1, 2, 2, 3, 3, 3]               # False: 1,2,3 but 2 appears twice
]

for i, nums in enumerate(test_cases, 1):
    result = unique_occurrences(nums)
    print(f"Test {i}: {nums} ? {result}")
Test 1: [6, 4, 2, 9, 4, 2, 2, 9, 9, 9] ? True
Test 2: [1, 2, 2, 1, 1, 3] ? True
Test 3: [1, 2, 2, 3, 3, 3] ? False

How It Works

Let's trace through the first example to understand the logic ?

nums = [6, 4, 2, 9, 4, 2, 2, 9, 9, 9]

# Step 1: Count occurrences
from collections import Counter
counts = Counter(nums)
print(f"Counts: {dict(counts)}")

# Step 2: Get occurrence values
occurrences = list(counts.values())
print(f"Occurrences: {occurrences}")

# Step 3: Check uniqueness
unique_occurrences = set(occurrences)
print(f"Unique occurrences: {unique_occurrences}")
print(f"Are all occurrences unique? {len(occurrences) == len(unique_occurrences)}")
Counts: {6: 1, 4: 2, 2: 3, 9: 4}
Occurrences: [1, 2, 3, 4]
Unique occurrences: {1, 2, 3, 4}
Are all occurrences unique? True

Comparison

Method Time Complexity Space Complexity Readability
Using Counter O(n) O(n) High
Manual Dictionary O(n) O(n) Medium

Conclusion

Use Counter from collections for cleaner code when checking if occurrence counts are unique. Both approaches have O(n) time complexity and work by comparing the length of occurrence counts with their unique set.

Updated on: 2026-03-25T11:46:09+05:30

355 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements