Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Arrays & Two Pointers/Two Pointers

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
Practise nextMajority Element
IdeaPracticePitfalls

Recognise it

The signal

element occurring more than n/2 (or n/k) times

  1. 1Brute force

    Count frequencies with a hash map.

  2. 2The observation

    O(n) memory: cancellation of distinct pairs preserves the majority.

  3. 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

Next

Majority Element

0/1 solved
  • Majority ElementAmazon, AdobeEasy

Pitfalls

Misconceptions

  • !the candidate is the majority without a verification pass when majority is not guaranteed
Go deeperresources

Resources

  • Two PointersUSACO Guide · course
  • Prefix SumsUSACO Guide · course
  • More on Prefix Sums (difference arrays)USACO Guide · course
  • Array cheatsheetTech Interview Handbook · article
  • NeetCode RoadmapNeetCode · sheet
  • Array and String explore cardLeetCode · course
TodayReviewPatternsProblems