题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
使用遍历+哈希搞定
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
};