题解 | #数组中出现次数超过一半的数字#

数组中出现次数超过一半的数字

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
};
全部评论

相关推荐

爱读书的放鸽子能手很...:刷个两端实习,冲春招,流水线什么时候不能去
我的秋招日记
点赞 评论 收藏
分享
10-14 12:20
门头沟学院 Java
迷茫的大四🐶:摊牌了,我是25届的,你们也不招我
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务