One value appears more than n/2 times. Instead of counting everything, Boyer–Moore voting keeps a single candidate and a count: matches vote it up, mismatches cancel it out, and the true majority always survives — O(n) time, O(1) space.
Given an array nums of size n, return the element that appears more than ⌊n/2⌋ times. You may assume it always exists. nums = [2,2,1,1,1,2,2] → 2 (it appears 4 times, and 4 > 7/2).
count = 0, no committed candidate yet.count === 0, take the current value as the new candidate— the old one has been fully cancelled out, so start fresh.count++; otherwise count--.candidate is the majority element.Counting with a Map is the obvious O(n)-time approach but it stores up to n distinct keys. Voting keeps only candidate and count, so it is O(1) space — the reason it's the textbook answer.
candidate and count = 0.1function majorityElement(nums: number[]): number {2▶ let candidate = 0;3▶ let count = 0;45 for (const x of nums) {6 if (count === 0) candidate = x; // adopt a fresh candidate7 count += x === candidate ? 1 : -1; // vote with it or against it8 }910 return candidate; // the > n/2 element always survives11}
function majorityElement(nums: number[]): number {
let candidate = 0;
let count = 0;
for (const x of nums) {
if (count === 0) candidate = x; // adopt a fresh candidate
count += x === candidate ? 1 : -1; // vote with it or against it
}
return candidate; // the > n/2 element always survives
}candidate and an integer count. The initial candidate value is irrelevant because count starts at 0, forcing an adoption on the first element.count === 0 the current candidate has been fully cancelled, so we commit to the value in front of us as the new candidate.count up; anything else pushes it down. Minority elements can drain it to 0but never below the majority's surplus.candidate is the answer.nums[n/2] (O(n log n)), or a randomized pick-and-verify.function majorityElement(nums: number[]): number {
let candidate = 0;
let count = 0;
for (const x of nums) {
if (count === 0) candidate = x; // adopt a fresh candidate
count += x === candidate ? 1 : -1; // vote with it or against it
}
return candidate; // the > n/2 element always survives
}The brute-force default: tally every value's frequency in a Map, then return whichever key crosses n/2.
function majorityElement(nums: number[]): number {
const counts = new Map<number, number>();
for (const x of nums) {
const c = (counts.get(x) ?? 0) + 1;
if (c > nums.length / 2) return x;
counts.set(x, c);
}
return nums[0]; // majority guaranteed to exist
}ndistinct keys — O(n) space. Boyer–Moore voting reaches the same answer with two scalars and O(1) space.If a value occupies more than half the array, after sorting it must cover the middle index, so nums[⌊n/2⌋] is the answer.
function majorityElement(nums: number[]): number {
nums.sort((a, b) => a - b);
return nums[Math.floor(nums.length / 2)];
}| "element appears > n/2 times" | Boyer–Moore voting |
| find the majority in O(1) space | candidate + count |
| count hits zero | adopt the current value |
| "> n/3 times" / two majorities | two candidates (Majority II) |
let candidate = 0;
let count = 0;
for (const x of nums) {
if (count === 0) candidate = x;
count += x === candidate ? 1 : -1;
}
return candidate;