Problem 1704
Question 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.
Solution
Main idea:
The most straightforward approach seems to be the most optimal one (among all submissions at least).
- Create a dictionary/counter to track the frequency of each element.
- Add each frequency captured by the counter to a set, returning False if a given frequency is already in the set
- If all frequencies are exhausted, return True.
Code:
1 | def uniqueOccurrences(self, arr: List[int]) -> bool: |
Comments:
Time complexity: O(n) for the creation of counter.
At the time of submission, the above solution passes leetcode submission cases in 37ms, beats 88.35% of users. It uses 17.46MB of memory, beating 41.07% of users.