Majority Element
169. Majority Element
Boyer-Moore Voting Algorithm
[7, 7, 5, 7, 5, 1 | 5, 7 | 5, 5, 7, 7 | 7, 7, 7, 7] => 7
[7, 7, 5, 7, 5, 1 | 5, 7 | 5, 5, 7, 7 | 5, 5, 5, 5] => 5def majorityElement(self, nums: List[int]) -> int:
count = 0
cand = None
for n in nums:
if count == 0:
cand = n
count += 1 if cand == n else -1
return cand229. Majority Element II
Last updated