#229

Majority Element II

Medium Extended Moore's voting

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return every value that appears more than n/3 times.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: all values that appear more than n/3 times (at most two).

Why this works: keep two candidates; a third different value cancels one vote from each. Then count the two candidates to confirm.

Example: [3, 2, 3]

candidates 3 and 2 survivecounts: 3 → 2 times > 1 ✓, 2 → 1 time ✗answer [3]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Skipping the verification pass; a candidate can survive voting without being a real majority.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Kadane, Dutch Flag & Moore's Voting
🎬 Episodes 11–13: video coming soon