public int findMode (int[] A, int[] B) {
// write code here
int m = A.length;
int n = B.length;
int[] merged = new int[m + n];
int i = 0, j = 0, k = 0;
// 合并两个牛群的体重
while (i < m && j < n) {
if (A[i] <= B[j]) {
merged[k++] = A[i++];
} else {
merged[k++] = B[j++];
}
}
// 将剩余的牛群体重加入合并后的数组
while (i < m) {
merged[k++] = A[i++];
}
while (j < n) {
merged[k++] = B[j++];
}
// 统计每个体重出现的次数
Map<Integer, Integer> count = new HashMap<>();
int maxCount = 0;
int mode = 0;
for (int weight : merged) {
count.put(weight, count.getOrDefault(weight, 0) + 1);
if (count.get(weight) > maxCount) {
maxCount = count.get(weight);
mode = weight;
} else if (count.get(weight) == maxCount) {
mode = Math.max(mode, weight);
}
}
return mode;
}