Blog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2025 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Algorithms
Algorithmsmedium

Boyer–Moore majority vote: what does it solve and what’s the core idea?

Tags
#majority-vote#array#linear-time#constant-space
Back to categoryPractice quiz

Answer

It finds the element that appears more than n/2 times (if it exists). The idea is to cancel pairs of different elements; the remaining candidate is the majority. It runs in O(n) time and O(1) space.

function majority(nums: number[]): number {
  let cand = 0;
  let count = 0;
  for (const x of nums) {
    if (count === 0) cand = x;
    count += x === cand ? 1 : -1;
  }
  return cand;
}

Related questions

Algorithms
What does Kadane’s algorithm solve?
#kadane#dynamic-programming#array
Algorithms
What is the two pointers technique?
#two-pointers#array#technique
Data Structures
Difference between Array and LinkedList?
#array#linked-list#comparison