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<>();
int index = 0;
while (index < arr.length) {
map.put(arr[index], map.getOrDefault(arr[index], 0) + 1);
index++;
}
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
list.sort(new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2) {
if (o1.getValue() < o2.getValue()) {
return 1;
} else if (o1.getValue() == o2.getValue()) {
return o2.getKey() - o1.getKey();
} else {
return -1;
}
}
});
HashSet<Integer> hashSet = new HashSet<>();
int amount = 0;
for (Map.Entry<Integer, Integer> myMap : list) {
hashSet.add(myMap.getValue());
amount++;
}
return hashSet.size() == amount;
}
}
}
本题考察的知识点主要是重复元素的统计,所用编程语言是java.我们可以利用HashMap统计重复元素个数,然后利用hashset添加元素对应重复个数,最后根据hashset大小来判断是否每种体重的牛的数量都是不同的

京公网安备 11010502036488号