题解 | #牛的体重统计#
牛的体重统计
https://www.nowcoder.com/practice/15276ab238c9418d852054673379e7bf
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param weightsA int整型vector
* @param weightsB int整型vector
* @return int整型
*/
int findMode(vector<int>& weightsA, vector<int>& weightsB) {
// write code here
int count = 0, maxNums = 0;
map<int, int> mp;
for (int i = 0; i < weightsA.size(); i++) {
mp[weightsA[i]]++;
if (mp[weightsA[i]] > count) {
maxNums = weightsA[i];
count = mp[weightsA[i]];
} else if (mp[weightsA[i]] == count && weightsA[i] > maxNums) {
maxNums = weightsA[i];
}
}
for (int i = 0; i < weightsB.size(); i++) {
mp[weightsB[i]]++;
if (mp[weightsB[i]] > count) {
maxNums = weightsB[i];
count = mp[weightsB[i]];
} else if (mp[weightsB[i]] == count && weightsB[i] > maxNums) {
maxNums = weightsB[i];
}
}
return maxNums;
}
};