DEV Community

Saranya R
Saranya R

Posted on

Find the Majority Element

Approach Explanation o(〃^▽^〃)o

  1. I used Boyer–Moore Voting to find a possible majority element.
  2. If the current number matched the candidate, I increased the count, otherwise, I decreased it.
  3. Reset when count = 0 → I picked a new candidate.
  4. Verification step → After one pass, I checked if the candidate actually appeared more than ⌊n/2⌋ times.
  5. If yes, return the candidate. Otherwise, return -1

Method Used: (⇢^▽^)⇢

  • Boyer–Moore Voting Algorithm
  • Verification step
  • Efficient runtime
  • Constant space

Top comments (0)