考察知识点:哈希
题目分析:
分别遍历一遍数组,记录每个体重出现的次数,然后维护出现次数的最大值即可。
所用编程语言:C++
#include <unordered_map>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param weightsA int整型vector
* @param weightsB int整型vector
* @return int整型
*/
int findMode(vector<int>& weightsA, vector<int>& weightsB) {
// write code here
unordered_map<int, int> mp;
int res = 0;
for (auto weight: weightsA) {
mp[weight]++;
if (mp[weight] >= mp[res])
res = mp[weight] == mp[res] ? max(res, weight) : weight;
}
for (auto weight: weightsB) {
mp[weight]++;
if (mp[weight] >= mp[res])
res = mp[weight] == mp[res] ? max(res, weight) : weight;
}
return res;
}
};

京公网安备 11010502036488号