Given an array of integers arr
, you need to determine if the number of occurrences of each value in the array is unique.
More precisely, for an array arr
, we have to check if the occurrences of each element in the array are all different. If the occurrences are unique, return True
, otherwise return False
.
Here is the complete Python code for the problem:
from collections import Counter
def uniqueOccurrences(arr):
# Step 1: Count occurrences of each element
element_count = Counter(arr)
# Step 2: Extract the occurrences
occurrences = list(element_count.values())
# Step 3: Check for unique occurrences
return len(occurrences) == len(set(occurrences))
# Example usage:
# arr = [1,2,2,1,1,3]
# The output should be True because the counts are {1: 3, 2: 2, 3: 1} and they are all unique.
print(uniqueOccurrences([1, 2, 2, 1, 1, 3])) # Output: True
This approach ensures that the problem is solved efficiently even for the maximum constraint size.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?