Majority Element
Pattern: Majority element (Boyer-Moore)
Boyer-Moore voting. Keep one candidate and a vote count. Matching values add a vote, other values cancel one, and a new candidate takes over when the count hits 0.
The problem
Given an array of n whole numbers, return the value that appears more than n/2 times. Such a value is promised to
exist. For example, [3, 2, 3] gives 3, and [2, 2, 1, 1, 1, 2, 2] gives 2.
The function name majorityElement matches LeetCode. Use the link above for the exact statement and limits.
Think first
- A hash map of counts solves it in O(n) time. Can you use O(1) memory?
- Pair each copy of the majority value with a copy of some other value, and remove both. What is left?
- How can one pass do that pairing with only two numbers?
Hints
Brute force, and why it fails
Count each value with a nested loop: O(n²). Sorting and taking the middle value is O(n log n). A hash map is O(n) time and O(n) memory. Voting is O(n) time and O(1) memory.
The key insight
The majority has more copies than all other values together, so it cannot be fully cancelled out. The vote counter does the cancelling in one pass.
Dry run
| candidate | 2 |
|---|---|
| count | 2 |
nums = [2, 2, 1, 1, 1, 2, 2]. The count starts at 0, so the first 2 becomes the candidate. The next 2 makes count 2.
The plan
candidate = none, count = 0
for v in nums:
if count == 0: candidate = v
if v == candidate: count = count + 1
else: count = count - 1
return candidate
Complexity
- Time
O(n)- Space
O(1)
One pass with two numbers.
Edge cases
- One element: that element.
- The majority shows up late, as in
[1, 2, 1, 2, 1]: it still survives. - Negative values: they work like any other value.
I've built my logic, show the code
Java
class Solution {
public int majorityElement(int[] nums) {
int candidate = nums[0];
int count = 0; // votes for the candidate not yet cancelled out
for (int v : nums) {
if (count == 0) candidate = v; // every earlier vote cancelled: pick a new candidate
count += (v == candidate) ? 1 : -1;
}
return candidate; // the majority survives all cancellations
}
}C++
class Solution {
public:
int majorityElement(vector<int>& nums) {
int candidate = nums[0];
int count = 0; // votes for the candidate not yet cancelled out
for (int v : nums) {
if (count == 0) candidate = v; // every earlier vote cancelled: pick a new candidate
count += (v == candidate) ? 1 : -1;
}
return candidate; // the majority survives all cancellations
}
};Python
class Solution:
def majorityElement(self, nums: list[int]) -> int:
candidate, count = nums[0], 0 # count: votes not yet cancelled out
for v in nums:
if count == 0:
candidate = v # every earlier vote cancelled: pick a new candidate
count += 1 if v == candidate else -1
return candidate # the majority survives all cancellationsCommon mistakes
- Using the vote without a promise that a majority exists. Then the survivor must be counted in a second pass.
- Taking a new candidate but forgetting to give it its own vote, so its count stays at 0.
- Returning the count instead of the candidate.