requirements
Give you an integer array arr, ask you to help count the occurrence of each number in the array.
Returns true if each occurrence is unique; Otherwise return false.
Example 1:
Input: arr = [1,2,2,1,1,3] Output: true Explanation: In this array,1 appears three times,2 twice, and 3 only once. No two numbers occur the same number of times.Copy the code
Example 2:
Input: arr = [1,2] output: falseCopy the code
Example 3:
Input: arr = [-3,0,1,-3,1,1, -3,10,0] Output: trueCopy the code
The core code
class Solution:
def uniqueOccurrences(self, arr: List[int]) - >bool:
from collections import Counter
dic = Counter(arr)
s = set(a)for key,val in dic.items():
if val in s:
return False
s.add(val)
return True
Copy the code
Get the number of occurrences of each element, loop through it, return False for the same number of occurrences, return True for the same number of occurrences.