This commit is contained in:
2026-03-27 21:48:51 +08:00
parent de39ce466a
commit 9b63dc45d3
3 changed files with 48 additions and 71 deletions
+17
View File
@@ -0,0 +1,17 @@
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
}