easyArrayHash TableArrays & Hashing

Majority Element

easyTime: O(n)Space: O(1)

Signals to notice

element appears more than n/2 timesfind the majorityBoyer-Moore voting

Brute force first

Count every element with a hash map. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

The key insight

Boyer-Moore voting: maintain a candidate and counter. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

What must stay true

The majority element survives cancellation because it appears more than all others combined. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Easy way to go wrong

Not knowing Boyer-Moore — the trick is that the majority element always wins the vote. The fix is usually to return to the meaning of each move, not just the steps themselves.

Arrays & Hashing Pattern