力扣打卡题
力扣题库1207
用map和Set实现: 1、map用于统计次数 2、set用于次数去重:
class Solution {
public boolean uniqueOccurrences(int[] arr) {
Map<Integer, Integer> counts = new HashMap<>();
for (int value : arr) {
counts.put(value, counts.getOrDefault(value, 0) + 1);
}
Set<Integer> glSet = new HashSet<>();
for(int count : counts.values()){
if(glSet.contains(count)){
return false;
} else {
glSet.add(count);
}
}
return true;
}
}