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

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

http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163

function MoreThanHalfNum_Solution(numbers)
{
    let result = numbers.reduce((temp, data) => {
        temp[data] = temp[data] ? temp[data] + 1 : 1;
        return temp;
    }, {})
    let num;
    for (let index in result) {
        if (result[index] > numbers.length/2) {
            num = index;
        }
    }
    return num;
}
module.exports = {
    MoreThanHalfNum_Solution : MoreThanHalfNum_Solution
};
全部评论

相关推荐

评论
1
1
分享
牛客网
牛客企业服务