Boyer-Moore Majority Vote Algorithm

Suppose we have a list of nn votes and we want to count which candidate received the most votes. The straightforward approach would be to iterate over the list, maintaining a count for the number of votes each candidate received, and then, once all of the votes have been processed, check and see which candidate has the highest count. This approach is both simple and always works, but there are scenarios in which we can determine the winner without needing to maintain individual counts for each candidate. The Boyer-Moore Majority Vote Algorithm is an algorithm for finding the strict majority winner (if one exists) with constant memory and a single pass over the list.

The algorithm is surprisingly simple: we just keep track of a running candidate and counter. The running counter represents the difference between how many times we’ve seen the candidate and how many times we’ve seen other elements. Each time we see a vote for the candidate, we increment the counter. Each time we see a vote for anyone else, we decrement it. If at any point the counter ever drops to zero, we discard the current candidate and choose the next element as the new candidate. After processing the entire list, the remaining candidate is the majority element.

Example execution flow of the Boyer-Moore algorithm. The elements along the x-axis indicate the votes as they are processed, while the shape of the point and the y-axis indicate the current candidate and count, respectively. (Image Credits: Eppstein, David . Wikimedia.org, 2024, upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Boyer-Moore_MJRTY.svg/1920px-Boyer-Moore_MJRTY.svg.png. Accessed 22 Feb. 2026.)
def boyer_moore(votes: list):
    candidate = None
    count = 0

    for v in votes:
        if count == 0:
            candidate = v
        count += 1 if v == candidate else -1

    return candidate

If that seems to good to be true, it’s because it is. The Boyer-Moore algorithm only works when a strict majority winner exists, that is, if the most frequent element occurs more than half the time. In general, it won’t find the most frequent element, and it also won’t verify if a strict majority winner exists without a second pass through the list. However, in the narrow cases in which it does work, the Boyer-Moore majority vote algorithm is incredibly simple to implement and efficient.

It’s also not too hard to understand. Since the count is incremented when the next vote matches the running candidate and decremented when the next vote is different from that candidate, then the count only hits 0 when the votes processed thus far can be organized into pairs of different elements so that they cancel out. For example, if the sequence of votes went v1,v1,v2,v3v_1, v_1, v_2, v_3, then we could pair them off as (v1,v2),(v1,v3)(v_1, v_2), (v_1, v_3). The running count, then, indicates the number of votes for the candidate that couldn’t be canceled by votes for other candidates up until. After processing all the votes, only the strict majority element can have a positive count, and so it must be the candidate at the end. (And the count at the end must be positive if there is a strict majority element.)


Reasources

  • GeeksforGeeks. “BoyerMoore Majority Voting Algorithm.” GeeksforGeeks, June 2021, www.geeksforgeeks.org/theory-of-computation/boyer-moore-majority-voting-algorithm/. Accessed 22 Feb. 2026.
  • Wikipedia Contributors. “Boyer–Moore Majority Vote Algorithm.” Wikipedia, Wikimedia Foundation, 1 Oct. 2019, en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm. Accessed 22 Feb. 2026.

Leave a Reply

Your email address will not be published. Required fields are marked *