使用遍历+哈希搞定
function MoreThanHalfNum_Solution(numbers)
{
// write code here
let target = Math.floor(numbers.length/2);
let hashMap = new Map();
for(let i of numbers){
hashMap.set(i, 1 + (hashMap.has(i) ? hashMap.get(i) : 0));
if(hashMap.get(i) > target) return i;
}
}
module.exports = {
MoreThanHalfNum_Solution : MoreThanHalfNum_Solution
};


京公网安备 11010502036488号