知识点:哈希表
这个题目不知道为啥标为中等题..
遍历数组,使用HashMap记录每种元素的出现次数
再遍历HashMap,使用一个HashSet来判断是否存在相同出现次数的元素,即判断HashMap的vlaue值有无重复。
Java题解如下:
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr int整型一维数组
* @return bool布尔型
*/
public boolean uniqueOccurrences (int[] arr) {
// write code here
Map<Integer, Integer> map = new HashMap<>();
for(int item: arr) {
map.put(item, map.getOrDefault(item, 0) + 1);
}
Set<Integer> set = new HashSet<>();
for(Map.Entry<Integer, Integer> entry: map.entrySet()) {
if(!set.add(entry.getValue())) {
return false;
}
}
return true;
}
}



京公网安备 11010502036488号