Technique
Boyer-Moore Majority Vote
Keep a candidate and a counter; pairing off different elements never removes a strict majority element, giving O(n) time and O(1) space.
Learn firstHash Map / Hash Set Lookup
0%
mastery
0/1 solved
0/1 solved
Recognise it
The signal
element occurring more than n/2 (or n/k) times
1Brute force
Count frequencies with a hash map.
2The observation
O(n) memory: cancellation of distinct pairs preserves the majority.
3Why it works
The majority element (if it exists) survives as the candidate after all cancellations.
Reach for it when
- +O(1) extra space required
Not when
- ×no majority guaranteed (a verification pass is required)
- ×need the mode, not a majority (hash map)
Practice
0/1 solved
- Majority ElementAmazon, AdobeEasy
Pitfalls
Misconceptions
- !the candidate is the majority without a verification pass when majority is not guaranteed