Skip to content

Latest commit

 

History

History
17 lines (16 loc) · 287 Bytes

面试题39. 数组中出现次数超过一半的数字.md

File metadata and controls

17 lines (16 loc) · 287 Bytes
func majorityElement(nums []int) int {
    x, votes := nums[0], 1
    for i := 1; i < len(nums); i++ {
        if votes == 0 {
            x = nums[i]
        }
        if nums[i] == x {
            votes ++
        }else {
            votes --
        }
    }
    return x
}