题解 | 牛的体重统计

牛的体重统计

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;
    }
}

全部评论

相关推荐

代码飞升:别这样贬低自己,降低预期,放平心态,跟昨天的自己比。做好自己,反而会效率更高心态更好,加油兄弟
点赞 评论 收藏
分享
牛客70961307...:你这项目认真的吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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