题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
function MoreThanHalfNum_Solution(numbers) { // write code here let nums = {}; for(let i = 0 ; i < numbers.length ; i++){ let key = numbers[i]; if(!nums[key]) nums[key]=0; nums[key]++; } let len = Math.floor(numbers.length/2); for(let i = 0 ; i < numbers.length ; i++){ if(nums[numbers[i]]>len) return numbers[i]; } return -1; } module.exports = { MoreThanHalfNum_Solution : MoreThanHalfNum_Solution };