题目:
题解:
- func majorityElement(nums []int) (ans int) {
- cnts := 0
- for _, v := range nums {
- if v == ans {
- cnts++
- } else if cnts == 0 {
- ans = v
- } else {
- cnts--
- }
- }
- return
- }