一、知识点:

HashMap

二、文字分析:

先将两个牛群的体重合并到一个数组中。然后,使用哈希表统计每个体重值的出现次数,同时跟踪出现次数最多的体重值以及其出现次数。

最后,返回出现次数最多的体重值作为结果。

三、编程语言:

java

四、正确代码:

import java.util.*;

public class Solution {
    public int findMode(int[] weightsA, int[] weightsB) {
        int m = weightsA.length;
        int n = weightsB.length;

        // 合并体重
        int[] weights = new int[m + n];
        System.arraycopy(weightsA, 0, weights, 0, m);
        System.arraycopy(weightsB, 0, weights, m, n);

        // 统计体重出现次数
        Map<Integer, Integer> countMap = new HashMap<>();
        int maxCount = 0;
        int mode = 0;

        for (int weight : weights) {
            int count = countMap.getOrDefault(weight, 0) + 1;
            countMap.put(weight, count);

            if (count > maxCount || (count == maxCount && weight > mode)) {
                maxCount = count;
                mode = weight;
            }
        }

        return mode;
    }
}