没看答案,首先利用哈希表统计元素出现次数,然后对次数进行排序,最后查验是否出现了重复。
from collections import defaultdict
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
freq = defaultdict(int)
for num in arr:
freq[num] += 1
freq = sorted(freq.items(), key=lambda x:x[1], reverse=True)
n = len(freq)
for i in range(1, n):
if freq[i][1] == freq[i-1][1]:
return False
return True
首先利用哈希表统计元素出现次数,然后利用新的哈希表对次数进行统计,并不断查验是否出现了重复。
from collections import defaultdict
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
freq_arr = defaultdict(int)
freq_freq = dict()
for num in arr:
freq_arr[num] += 1
for fq in freq_arr.values():
if fq in freq_freq.keys():
return False
else:
freq_freq[fq] = 1
return True