Majority Element
beginnerSolution
Highlighted line tracks the current step.
1def majority_element(arr):
2 candidate, count = None, 0
3 for num in arr:
4 if count == 0:
5 candidate = num
6 elif num == candidate:
7 count += 1
8 else:
9 count -= 1
10 verify = arr.count(candidate)
11 return candidate if verify > len(arr)//2 else -1
Define the function