给你一个非递减的 有序 整数数组,已知这个数组中恰好有一个整数,它的出现次数超过数组元素总数的 25%。
请你找到并返回这个整数
示例 1:
输入:arr = [1,2,2,6,6,6,6,7,10]
输出:6
提示
func findSpecialInteger(arr []int) int {
l := len(arr)
if l == 0 {
return 0
}
res,count := 0, 0
for i := 0; i < l; i++ {
if count == 0 {
res = arr[i]
count++
}else if arr[i] == res {
count++
}else {
count--
}
if count > l / 4 {
break
}
}
return res
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。