Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Unique Number of Occurrences in Python
Suppose we have an array, and we need to check whether each element has a unique number of occurrences. If no such element exists, we return false; otherwise, we return true. For example, given the array [1, 1, 2, 2, 2, 3, 4, 4, 4, 4], the function will return true because no two elements have the same number of occurrences: 1 occurs twice, 2 occurs three times, 3 occurs once, and 4 occurs four times.
Using Dictionary to Track Occurrences
A dictionary is ideal for counting occurrences since it stores key-value pairs where keys are unique elements and values are their frequencies ?
def unique_occurrences(arr):
# Count occurrences of each element
counts = {}
for num in arr:
counts[num] = counts.get(num, 0) + 1
# Check if all occurrence counts are unique
frequencies = {}
for count in counts.values():
if count in frequencies:
return False
frequencies[count] = 1
return True
# Test examples
print(unique_occurrences([1, 1, 2, 2, 2, 3, 4, 4, 4, 4]))
print(unique_occurrences([1, 2, 3]))
print(unique_occurrences([1, 1, 2, 2]))
True True False
Using Collections Counter
The Counter from collections module automatically counts occurrences and returns a dictionary-like object ?
from collections import Counter
def unique_occurrences_counter(arr):
counts = Counter(arr)
# Check if all count values are unique by comparing set size
return len(set(counts.values())) == len(counts.values())
# Test examples
print(unique_occurrences_counter([1, 1, 2, 2, 2, 3, 4, 4, 4, 4]))
print(unique_occurrences_counter([1, 2, 3]))
print(unique_occurrences_counter([1, 1, 2, 2]))
True True False
Using Set and Manual Counting
This approach uses dictionary comprehension with set() to get unique elements and manually count occurrences ?
def unique_occurrences_set(arr):
# Count occurrences for each unique element
counts = {num: arr.count(num) for num in set(arr)}
# Check if all occurrence counts are unique
return len(set(counts.values())) == len(counts.values())
# Test examples
print(unique_occurrences_set([1, 1, 2, 2, 2, 3, 4, 4, 4, 4]))
print(unique_occurrences_set([1, 2, 3]))
print(unique_occurrences_set([1, 1, 2, 2]))
True True False
Using Two Lists Approach
This method tracks unique elements and their counts in separate lists ?
def unique_occurrences_lists(arr):
unique_elements = []
counts = []
# Find unique elements
for num in arr:
if num not in unique_elements:
unique_elements.append(num)
# Count occurrences of each unique element
for element in unique_elements:
counts.append(arr.count(element))
# Check if all counts are unique
unique_counts = []
for count in counts:
if count in unique_counts:
return False
unique_counts.append(count)
return True
# Test examples
print(unique_occurrences_lists([1, 1, 2, 2, 2, 3, 4, 4, 4, 4]))
print(unique_occurrences_lists([1, 2, 3]))
print(unique_occurrences_lists([1, 1, 2, 2]))
True True False
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Dictionary | O(n) | O(n) | Most efficient approach |
| Counter | O(n) | O(n) | Clean, readable code |
| Set + Manual | O(n²) | O(n) | Simple understanding |
| Two Lists | O(n²) | O(n) | Educational purposes |
Conclusion
The Counter method provides the cleanest solution, while the dictionary approach offers the most control. Both are efficient with O(n) time complexity for checking unique occurrences in an array.
