1207. 独一无二的出现次数
解题思路
- 首先使用hashmap 存储每一个数组元素的出现次数
- 然后创建一个hashset
- 增强for循环遍历hashmap 查看hashSet中是否存在该value
class Solution {
public boolean uniqueOccurrences(int[] arr) {
Map<Integer,Integer> map = new HashMap<>();
int[] nums = new int[2001];
for(int i = 0; i < arr.length; i++){
map.put(arr[i],0);
}
for(int k = 0; k < arr.length; k++){
int temp = map.get(arr[k]) + 1;
map.replace(arr[k],temp);
}
Set<Integer> set = new HashSet<>();
for(int count: map.values()){
if(set.contains(count)){
return false;
}
else{
set.add(count);
}
}
return true;
}
}