LeetCode 1207: Unique Number of Occurrences

link

We can create a map: {value -> count}. Each value having unique number of occurrences means: in the map, each value has a distinct count. In other words, the number of different counts in the map is same as the map size.

Time: \mathcal{O}(n), space: \mathcal{O}(\texttt{unique-value-count}).

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        val_map = {}
        for v in arr:
            val_map[v] = val_map.get(v, 0) + 1
        
        return len(val_map) == len(set(val_map.values()))

Leave a comment