-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboyer_moore.py
47 lines (40 loc) · 1.14 KB
/
boyer_moore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Implementation of the "Boyer-Moore" majority
vote algorithm (https://en.wikipedia.org/wiki/Boyer-Moore_majority_vote_algorithm).
It aims at finding the element with the majority
count (called majority element) in a sequence
of values.
Time complexity: O(n)
Space complexity: O(1)
"""
def find_majority_elt(arr):
# Find majority candidate using algorithm
majority_candidate = -1
votes = 0
for elt in arr:
if votes == 0:
majority_candidate = elt
votes = 1
else:
if elt == majority_candidate:
votes += 1
else:
votes -= 1
return majority_candidate
def is_majority(candidate, array):
# Confirm that candidate is majority element
n = len(array)
if n == 0:
return False
count = 0
for elt in array:
if elt == candidate:
count += 1
return count > n // 2
values = [1, 1, 1, 2, 1, 3]
majority_elt = find_majority_elt(values)
# The below prints: Majority element is: 1
if is_majority(majority_elt, values):
print("Majority element is:", majority_elt)
else:
print("No majority element found.")