169. Majority Element
π© Easy
Question
Input: [3,2,3]
Output: 3Input: [2,2,1,1,1,2,2]
Output: 2Boyer Moore Voting Algorithm
Complexity
Code
Last updated
π© Easy
Input: [3,2,3]
Output: 3Input: [2,2,1,1,1,2,2]
Output: 2Last updated
def majorityElement(self, nums: List[int]) -> int:
count = 0
n = None
for num in nums:
if count == 0:
n = num
count += (1 if num == n else -1)
return n