前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >migratoryBirds * output int arr element that occus frequence is most high and

migratoryBirds * output int arr element that occus frequence is most high and

原创
作者头像
doc2
发布2025-05-31 11:41:49
发布2025-05-31 11:41:49
570
举报
文章被收录于专栏:Java技术Java技术
代码语言:java
复制
/**
 * 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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档