One value appears more than half the time in this list. Find it — without a hash map, without sorting, in a single pass, using just two variables. The Boyer-Moore majority vote treats the scan like an election where every different vote cancels a candidate’s. If a value truly holds the majority, it can never be fully cancelled: it’s the last one standing.
O(n) time · O(1) space · one pass · streaming
majority
A value that occurs strictly more than n/2 times.
candidate
The current front-runner we’re tracking. One variable.
count
Net votes for the candidate. Hits zero → adopt the next element.
cancellation
A different value cancels one candidate vote. The core trick.
majority.js — two variables, one pass
Ready
A list where one value holds the majority. We track a single candidate and a count. Press Step: matches add a vote, mismatches cancel one, and when count hits 0 we adopt whoever’s next.
–
candidate
0
count
How it works
Imagine pairing off elements: every time you see a value different from your candidate, the two annihilate — one vote each, gone. A true majority element has more copies than everything else combined, so no matter how the pairings fall, some of it always survives to the end. That surviving value is the answer.
1
Count is zero → adopt
If count == 0, the current element becomes the new candidate with count = 1. The old candidate was fully cancelled.
2
Match → add a vote
If the element equals the candidate, count += 1. The lead grows.
3
Mismatch → cancel
Otherwise count -= 1 — this element cancels one of the candidate’s votes. Nobody new is adopted yet.
✓
Survivor is the majority
The candidate left standing at the end is the majority element (verify with a second pass if one isn’t guaranteed to exist).
Time
O(n)
Space
O(1)
Passes
1 (+1 to verify)
vs hash map
O(1) not O(k) space
The code
# majority element (> n/2), O(n) time, O(1) spacedef majority(a):
cand, count = None, 0
for x in a:
if count == 0:
cand, count = x, 1
elif x == cand:
count += 1
else:
count -= 1
return cand # verify with a 2nd pass if unsure it exists
Two variables, no allocation — which is why it works on streams too, where you can’t store the whole input. The Boyer-Moore idea generalizes: to find all elements appearing more than n/3 times, keep two candidates and two counters.
Quick check
1. Why can a true majority element never be fully cancelled?
Exactly — each cancellation removes one majority vote and one non-majority vote, and there simply aren’t enough non-majority elements to eliminate a true majority.
2. What’s the advantage over counting with a hash map?
Both are O(n) time, but the hash map stores a count per distinct value (O(k) space). Boyer-Moore needs only a candidate and a counter.
FAQ
What is the majority element?
A value that appears strictly more than n/2 times in a list of n elements. At most one such value can exist.
What if no majority exists?
The algorithm still returns some candidate, but it may be wrong. If a majority isn’t guaranteed, add a second O(n) pass to count the candidate and confirm it exceeds n/2.
Does it work on a stream?
Yes — it only keeps two variables and looks at each element once, so it works even when you can’t store the whole input. That’s a defining property of streaming algorithms.
Can it find elements appearing more than n/3 times?
Yes, with the generalized Boyer-Moore: track two candidates and two counters. There can be at most two such values, and the same cancellation logic finds them.
The picture: votes cancel in pairs
The array [3, 3, 4, 2, 4, 4, 2, 4, 4]. The candidate and count are shown after each element; a marked bar under an element means it cancelled a vote. The candidate flips from 3 to 4 when the count hits zero, and the 4s outnumber everything else so the final candidate survives.
Worked example, step by step
Run the vote on A = [3, 3, 4, 2, 4, 4, 2, 4, 4], nine elements. The rule: if count is 0, adopt the current element as candidate; otherwise add one if it matches the candidate and subtract one if it does not.
i
x
Rule applied
candidate
count
0
3
count was 0: adopt
3
1
1
3
matches candidate: +1
3
2
2
4
mismatch: −1
3
1
3
2
mismatch: −1
3
0
4
4
count was 0: adopt
4
1
5
4
matches candidate: +1
4
2
6
2
mismatch: −1
4
1
7
4
matches candidate: +1
4
2
8
4
matches candidate: +1
4
3
At index 3 the 2 cancels the last 3-vote and the count reaches 0: the pairs (3, 4) and (3, 2) have cancelled, leaving the 3s with no surplus. The very next element (4) starts a fresh candidacy. The final candidate 4 appears 5 times out of 9. Every cancellation removes one vote for the candidate and one against it, so a value with more than n/2 copies cannot be cancelled out; here the 3s lost (2 copies) while the 4s survived with a final count of 3. Note that the count is not the number of occurrences: the 4 occurs 5 times but ends at 3.
The n/3 version, traced. With two candidates and two counters, run [1, 1, 1, 3, 3, 2, 2, 2]. The three 1s fill the first slot (count 3). The 3s take the second slot (count 2). The first 2 matches neither and both slots are full, so both counts drop: 1 to 2, 3 to 1. The second 2 drops them again, to 1 and 0. The third 2 finds the second slot empty and takes it. The candidates are 1 and 2. The verification pass counts each: 1 appears 3 times and 2 appears 3 times, and both beat 8 // 3 = 2, so both are answers. The 3s were real contenders for a while, but with only 2 copies the cancellations wore them down.
Complexity and trade-offs
Case
Time
Space
Why
Boyer-Moore vote
O(n)
O(1)
One pass, two variables. Works on a stream. Needs a second pass if a majority is not guaranteed.
Hash map of counts
O(n)
O(k)
k is the number of distinct values. Simple and verifies as it goes, but memory grows with the data.
Sort, take the middle
O(n log n)
O(1) to O(n)
A majority must occupy the middle position. Slower, and it needs the whole array.
Random pick + verify
O(n) expected
O(1)
Each guess is right with probability above 1/2, so under two tries on average. Randomised and needs a count pass per try.
Bit counting
O(n·w)
O(1)
Per bit position, the majority bit of the majority value is the majority bit overall. w is the word size, 32 or 64.
The vote wins on memory and on being a single pass over a stream. It loses on one thing: it always names a candidate, whether or not a majority exists. The hash map answers “is there one?” directly; the vote needs the verification pass to answer the same question.
Common mistakes
Trusting the candidate without verifying
If a majority is not promised, the vote still returns something. On [1, 2, 3] it returns 3, which appears once out of three times. A second pass that counts the candidate and compares with n/2 is required.
Mixing up “more than half” and “half”
A majority is strictly greater than n/2. On [1, 1, 2, 2] nothing qualifies, yet the vote returns 1. Verify with count > n // 2, not >=.
Adopting with count 0 instead of 1
When a new candidate is adopted it already holds one vote. Setting the count to 0 on adoption makes the algorithm forget it: on [1, 1, 2] it returns 2 although 1 is the majority.
Extending to n/3 with a single candidate
Elements that appear more than n/3 times need two candidates and two counters (at most two can exist). When a third value arrives and both slots are occupied, decrement both. Skip the final verification and [1, 2, 3, 4] reports candidates [4, 2], neither of which occurs more than 4/3 times.
Using it to find the most frequent element
The vote finds a majority, not a mode. On [1, 1, 2, 3, 4, 5] the value 1 is the most frequent (twice) but not a majority, and the vote returns 4.
Where this shows up
Streaming heavy hitters
The Misra–Gries summary (1982) generalises the vote to k−1 counters and finds every item above n/k occurrences in one pass. It is the standard first-line tool for “which keys dominate this log or traffic stream?”.
Distributed and parallel counting
Two partial results (candidate, count) merge by adding counts when candidates match and subtracting the smaller from the larger otherwise. Each worker scans its shard, then the small summaries are combined, followed by a verification pass.
Interview staple
“Majority element” and its n/3 follow-up are standard array questions, and the O(1)-space requirement is the reason the vote is the expected answer rather than a hash map.
Original source
The algorithm is the MJRTY “fast majority vote” of Robert Boyer and J Strother Moore, also known for the Boyer–Moore string search. The two are unrelated algorithms by the same authors.
SOLVE IT YOURSELF
Solve it: find the majority element
One counter, one pass, constant memory — and it finds the element that appears more than half the time. Write it in Python or TypeScript, running for real in your browser.
YOUR TASK
Implement majority(nums): return the value appearing strictly more thanlen(nums) / 2 times, or None (null in TypeScript) if there is none. Use the vote algorithm, then verify — the vote alone does not prove a majority exists.
HINTS — 5 IDEAS
Hold a candidate and a count. When count hits 0, adopt the current value as the new candidate.
Same value as the candidate? count += 1. Different? count -= 1 — a pairing-off of one vote against one opposing vote.
Why it works: a true majority element outnumbers everything else combined, so it cannot be fully cancelled out.
The vote always returns something for a non-empty list, even when no majority exists — so a second pass to count the candidate is mandatory, not optional.
Verify with count * 2 > len(nums), which avoids integer-division traps on odd lengths.
CPython · WebAssembly✓ Solved
1
1
▶ Watch it explained
Boyer-Moore Majority Vote: Time and Space Complexity, From First Principles