Own Code:
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
arr_dict = {}
result = []
for item in arr:
if arr_dict.get(f'{item}') == None:
arr_dict.update({f'{item}':1})
else:
arr_dict[f'{item}'] += 1
for value in arr_dict.values():
result.append(value)
#print(arr_dict)
return True if len(result) == len(set(result)) else False
Better Code:
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
count_dict = {}
# 统计每个数字出现的次数
for item in arr:
count_dict[item] = count_dict.get(item, 0) + 1
# 检查出现次数是否唯一
return len(count_dict.values()) == len(set(count_dict.values()))