/**
* migratoryBirds
* output int arr element that occus frequence is most high and rank at the lowest down layer
* constriants:
* arrList numberSize: 5<=n<=2*10to5
* it is guaranteed that each arr element is 1,2,3,4,5
* @param arr
* @return
*/
public static int migratoryBirds(List<Integer> arr){
if (arr==null){
return 0;
}
if (arr.isEmpty()){
return 0;
}
int targetSize=1;
for (int i = 0; i < 5; i++) {
targetSize*=10;
}
if (arr.size()<5 || arr.size()>targetSize){
return 0;
}
ArrayList<Integer> integers = new ArrayList<>();
integers.add(1);
integers.add(2);
integers.add(3);
integers.add(4);
integers.add(5);
for (Integer integer : arr) {
if (!(integers.contains(integer))){
return 0;
}
}
HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>();
for (Integer integer : arr) {
if (integerIntegerHashMap.containsKey(integer)){
integerIntegerHashMap.put(integer,integerIntegerHashMap.get(integer)+1);
}else {
integerIntegerHashMap.put(integer,1);
}
}
ArrayList<CustomerIntStatistic> customerIntStatistics = new ArrayList<>();
integerIntegerHashMap.forEach((k,v)->{
CustomerIntStatistic customerIntStatistic = new CustomerIntStatistic();
customerIntStatistic.setId(UUID.randomUUID().toString());
customerIntStatistic.setArrIntEle(k);
customerIntStatistic.setArrIntEleOccusFrequence(v);
customerIntStatistics.add(customerIntStatistic);
});
Collections.sort(customerIntStatistics, new Comparator<CustomerIntStatistic>() {
@Override
public int compare(CustomerIntStatistic o1, CustomerIntStatistic o2) {
if (o1.getArrIntEleOccusFrequence()>o2.getArrIntEleOccusFrequence()){
return 1;
} else if (o1.getArrIntEleOccusFrequence()<o2.getArrIntEleOccusFrequence()) {
return -1;
} else if (o1.getArrIntEleOccusFrequence()==o2.getArrIntEleOccusFrequence()) {
if (o1.getArrIntEle()<o2.getArrIntEle()){
return 1;
} else if (o1.getArrIntEle()>o2.getArrIntEle()) {
return -1;
}
}
return 0;
}
});
return customerIntStatistics.get(customerIntStatistics.size()-1).getArrIntEle();
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。