How the Boyer-Moore vote finds a majority candidate

Short answer: The Boyer-Moore majority vote finds a candidate that is the only value that could be a strict majority of an array, using one counter and one candidate. It scans once. On a match it increments. On a mismatch it decrements. At zero it takes the current value as the new candidate. A second pass counts the candidate and checks that it appears more than n/2 times. The first pass alone does not prove a majority. Together the passes are O(n) time and O(1) extra memory.


What the problem actually asks

If a value occupies more than half the positions, it cannot be cancelled by pairing each of its occurrences against a different value. The counter is that pairing. Whenever the counter returns to zero, a whole prefix has been paired away, and any majority of the whole array is still a majority of what remains. The surviving candidate is therefore the only possible majority element.

If there is no majority, the same pairing still produces some candidate. The first pass has no way to know. The second pass counts. Skipping it reports a winner in an array that has none.


What the algorithm guarantees

If a strict majority element exists, the first pass returns it. If none exists, the first pass returns an arbitrary survivor. The second pass distinguishes the cases by an exact count. Time is Θ(n) and extra memory is two integers. The algorithm finds an element that occurs more than n/2 times, not the mode of a multiset that is merely the most frequent. A value at exactly n/2 is not a strict majority and must fail the second pass.

A generalization keeps k−1 candidates to find every value that occurs more than n/k times. That is the Misra-Gries summary. The k = 2 case is this vote.


The working steps

  1. Set count to 0 and leave the candidate unset.
  2. For each value, if count is 0, set the candidate to the value and the count to 1. Else if the value equals the candidate, increment. Else decrement.
  3. After the scan, count the occurrences of the candidate in a second pass.
  4. If the count is greater than n/2, return the candidate. Otherwise report that no majority exists.
  5. Do not skip the second pass because the problem statement promised a majority. A later caller will reuse the function on data that has none.

The O(1) memory bound is for one majority candidate. The mode of an unrestricted multiset cannot be found in one pass with constant memory in the comparison model. Misra-Gries is the heavy-hitters generalization. The vote is not a sort and not a median. The median can be found in linear time by quickselect and is a different element.


Approaches that look simpler and fail

Approach Why it fails when the first pass is trusted without a count
Return the candidate immediately An array with no majority still produces a candidate. The second pass is the proof.
Accept a count of exactly n/2 A strict majority is more than half. A tie between two values is not a majority for either.
Decrement on a match The pairing then cancels the value against itself. The candidate of a real majority can be lost.
Use the algorithm to find the most common value below n/2 No such value is guaranteed to survive the pairing. The theorem is only about a strict majority.

Heavy hitters above 1/k are Misra-Gries. A median is quickselect. The Boyer-Moore vote is the constant-memory majority candidate plus a verifying count.


A working example

The Bash script and the Python script run the Boyer–Moore majority vote, then count the candidate. Both print 2, the only value that occurs more than n/2 times.

a=(2 2 1 2 3 2 2)
count=0
cand=0
for v in "${a[@]}"; do
  if (( count == 0 )); then cand=$v; count=1
  elif (( v == cand )); then count=$((count+1))
  else count=$((count-1)); fi
done
seen=0
for v in "${a[@]}"; do
  if (( v == cand )); then seen=$((seen+1)); fi
done
if (( seen * 2 > ${#a[@]} )); then echo "$cand"; else echo none; fi
a = [2, 2, 1, 2, 3, 2, 2]
count = 0
cand = None
for v in a:
    if count == 0:
        cand, count = v, 1
    elif v == cand:
        count += 1
    else:
        count -= 1
assert a.count(cand) > len(a) / 2
print(cand)

Where it is used

The Boyer-Moore vote finds a majority element in one pass and constant memory: a value that occupies more than half of a stream, if one exists. A second pass confirms it. Misra-Gries is the generalization that tracks several heavy hitters.


When you can drop this algorithm

Drop the verifying pass only when a majority is an invariant of the input and a wrong answer is acceptable if that invariant was already broken. Otherwise count.


Why this stays published

We publish this so a search for the Boyer-Moore majority vote returns the pairing counter and the second pass that proves the majority. The Boyer-Moore vote finds the only possible strict majority in one counter pass, and a second counting pass is what confirms the candidate really exceeds n/2.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

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