mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-06-04 02:21:15 +08:00
18 lines
213 B
Go
18 lines
213 B
Go
package main
|
|
|
|
func majorityElement(nums []int) int {
|
|
a, s := nums[0], 1
|
|
for i := 1; i < len(nums); i++ {
|
|
if a == nums[i] {
|
|
s++
|
|
continue
|
|
}
|
|
s--
|
|
if s == 0 {
|
|
a = nums[i]
|
|
s = 1
|
|
}
|
|
}
|
|
return a
|
|
}
|