luoheng
10/7/2019 - 2:58 AM

majorityElement

func majorityElement(nums []int) int {
    num, times := nums[0], 0
    for _, n := range nums {
        if n == num {
            times++
        } else {
            times--
            if times == 0 {
                num, times = n, 1
            }
        }
    }
    return num
}