169. 多数元素 - 力扣(LeetCode)
class Solution:
def majorityElement(self, nums: List[int]) -> int:
candidate = None
count = 0
for num in nums:
if count == 0: # 更新候选元素
candidate = num
count += (1 if num == candidate else -1)
return candidate