题解 | 牛的体重统计

牛的体重统计

https://www.nowcoder.com/practice/15276ab238c9418d852054673379e7bf

import java.util.*;


public class Solution {
    public int findMode (int[] weightsA, int[] weightsB) {
        final int m = weightsA.length, n = weightsB.length;
        int[] combined = new int[m + n];
        System.arraycopy(weightsA, 0, combined, 0, m);
        System.arraycopy(weightsB, 0, combined, m, n);
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : combined) {
            map.merge(i, 1, Integer::sum);
        }
        int maxKey = 0, maxVal = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            final int key = entry.getKey();
            final int val = entry.getValue();
            if (val > maxVal || (val == maxVal && key > maxKey)) {
                maxKey = key;
                maxVal = val;
            }
        }
        return maxKey;
    }
}

全部评论

相关推荐

10-15 10:23
门头沟学院 Java
牛可乐的头像真牛:赶紧举报,这公司绝对是诈骗的,等你签约后工作一两个月后根据合同漏洞把你开除,并且要求你赔偿3w培训费,996是为了提前筛选心甘情愿签下合同容易受骗的群体,纯粹面向校招生精心设计的骗局
你见过哪些工贼行为
点赞 评论 收藏
分享
26应届求职ing:你这是报了豆音四哥的班?双非本硕拿这两个项目写简历里投100多家嵌软也没什么面试,感觉项目简单了,很多人用
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务