剑指offer面试题39-数组中出现次数超过一半的数字

剑指offer面试题39:数组中出现次数超过一半的数字

数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
function MoreThanHalfNum_Solution(numbers){
// write code here
let arr = []
for(let i = 0; i < numbers.length; i++){
arr[numbers[i]] === undefined ? arr[numbers[i]] = 1 : arr[numbers[i]] += 1
}
for(let i = 0; i < arr.length; i++){
if((arr[i] || 0) > ((numbers.length)/2)){
return i
}
}
return 0
}
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2021 AURORA_ZXH
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信