Skip to content

visual walkthrough

Majority Element

EasyBoyer-Moore VotingReported at: AmazonMicrosoftApple+5

Introduces an O(1)-space voting trick that surprises most candidates.

Solve on LeetCode

The idea

If one value appears more than half the time, it can outvote every other value combined. Pair each vote for it with a vote against it: whatever is left standing must be the majority.

Boyer–Moore does exactly that with a single candidate and a counter, no map needed.

Complexity

approachtimespace
Count with a mapO(n)O(n)
Boyer–Moore votingO(n)O(1)

More walkthroughs