array and hashmap November 30, 2022

Unique number of occurrences

Time O(n) Space O(n) Open original problem

We will count the number of occurrences of each number in the array. We will then check if the number of occurrences is unique using a hash set.

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        count = collections.Counter(arr)
        return len(count.values()) == len(set(count.values()))

Time complexity: O(n), n is the length of the array
Space complexity: O(n)