1207. Unique Number of Occurrences
Содержание
Problem Statement
Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.
Naive Solution
A naive approach would be to create a dictionary to store the count of each integer, then iterate over the dictionary and compare each count with the counts of other integers. This approach requires O(n^2) time complexity, where n is the length of the array. This is not efficient for large input sizes.
Hints & Tips
To solve this problem efficiently, we can use Python’s built-in Counter class from the collections module. A Counter is a dictionary subclass for counting hashable objects. It’s a collection where elements are stored as dictionary keys, and their counts are stored as dictionary values.
Approach
- Create a Counter object from the input array.
- Convert the Counter object to a dictionary.
- Convert the dictionary values to a set.
- Compare the size of the set with the size of the dictionary. If they are equal, return true. Otherwise, return false.
Steps
- Import the Counter class from the collections module.
- Create a Counter object from the input array
arr
. - Convert the Counter object to a dictionary
dict_counts
. - Convert the dictionary values to a set
unique_counts
. - Compare the size of the set with the size of the dictionary. If they are equal, return true. Otherwise, return false.
Solution
from collections import Counter
def uniqueOccurrences(arr):
# Create a Counter object from the input array
dict_counts = dict(Counter(arr)) # {1: 3, 2: 2, 3: 1}
# Convert the dictionary values to a set
unique_counts = set(dict_counts.values())
# Compare the size of the set with the size of the dictionary
return len(unique_counts) == len(dict_counts)
This function uses the Counter class to count the occurrences of each integer in the input array. It then converts the Counter object to a dictionary and the dictionary values to a set. Finally, it compares the size of the set with the size of the dictionary.
If they are equal, it returns true. Otherwise, it returns false.