Majority Element
Table of Contents + −
This is one of those questions that looks easy. You count things. You find the winner. Done. But the interviewer is not really testing counting. They want to see if you can do it without using extra memory. So the real test here is space. Can you find the most common element while using almost no extra space? Let us talk through it.
🗳️ The Problem
You are given an array of numbers. One element appears more than half the time. We call this the majority element. The majority element is the value that shows up more than n/2 times. Here n is the size of the array. The problem promises us that such an element always exists. So we do not have to worry about the case where there is no winner.
Here is what the input and output look like:
Input: [2, 2, 1, 1, 1, 2, 2]Output: 2
Explanation: The array has 7 elements. The value 2 appears 4 times.4 is more than 7/2, so 2 is the majority element.🧮 The Simple Approach: Count Everything
The first idea most people get is to count. Go through the array. Keep a tally of how many times each number appears. Then pick the number whose count is more than n/2.
To keep the counts, we use a hash map. A hash map is a small table that stores each number together with how many times we have seen it. So you walk the array once. For every number you add one to its count in the map.
This works fine. It runs in O(n) time because you look at each element once. But there is a catch. You are storing a count for every different number. So in the worst case the map holds many entries. That costs O(n) extra space. The interviewer will usually nod and then ask the real question. “Can you do this without the extra map?” That is where the better idea comes in.
🤝 The Optimal Approach: Boyer-Moore Voting
Here is the clever trick. It is called the Boyer-Moore voting algorithm. The Boyer-Moore voting algorithm finds the majority element using just one candidate value and one counter. So no map at all.
Think of it like an election in a room. Imagine people holding signs with their number. Now pair up people who hold different numbers, and make both of them leave. Each pair that leaves removes one majority vote and one non-majority vote. So the pairs cancel out. The majority element appears more than half the time. So it has more people than everyone else put together. No matter how many pairs cancel, at least one person from the majority group is always left standing at the end.
We turn that idea into code with two variables. We keep a candidate, which is our current guess for the winner. And we keep a count, which is how strong that guess is right now. See how it flows:
- When
countis zero, we have no guess. So we pick the current number as the newcandidate. - When the current number is the same as the
candidate, that is a supporting vote. So we add one tocount. - When the current number is different, that is an opposing vote. So we subtract one from
count. This is the “pair cancels out” step.
By the end, the candidate left standing is the majority element.
Steps to find the majority element
- Start with
countset to 0 and nocandidateyet. - Walk through the array one number at a time.
- If
countis 0, set the current number as thecandidate. - If the current number equals the
candidate, add 1 tocount. - Otherwise, subtract 1 from
count. - After the loop, the
candidateis your answer.
# Boyer-Moore voting: one candidate, one countdef majority_element(nums): candidate = None count = 0
for num in nums: # No active guess, so adopt the current number if count == 0: candidate = num # Supporting vote adds 1, opposing vote subtracts 1 if num == candidate: count += 1 else: count -= 1
return candidate
nums = [2, 2, 1, 1, 1, 2, 2]print("Majority element:", majority_element(nums))The output of the above code will be:
Majority element: 2⏱️ Time and Space Complexity
Both methods look at every element once. So the time is the same. The big difference is space. The map method stores counts for many numbers. The voting method stores just two variables. See the comparison:
| Approach | Time | Space |
|---|---|---|
| Hash map counting | O(n) | O(n) |
| Boyer-Moore voting | O(n) | O(1) |
Tip
The voting trick only works because the problem promises a majority element exists. If that promise is not there, you must add a second pass. That pass counts the final candidate and confirms it really appears more than n/2 times. Say this out loud in the interview. It shows you understand why the trick works.
🧩 Key Takeaways
- ✅ The majority element appears more than
n/2times, and this problem guarantees one exists. - ✅ The hash map method is simple and runs in O(n) time, but it costs O(n) extra space.
- ✅ The Boyer-Moore voting algorithm gives the answer in O(n) time using only O(1) space.
- ✅ The idea is that opposing votes cancel in pairs, so the majority value is always left standing.
- ✅ Without a guaranteed majority, add a verification pass to confirm the candidate is real.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does 'majority element' mean in this problem?
Why: The majority element is the value that shows up more than half of the time, that is more than n/2 times.
- 2
Why is the Boyer-Moore voting algorithm better than the hash map method here?
Why: Both run in O(n) time, but voting uses only two variables, so its space is O(1) instead of O(n).
- 3
In the voting algorithm, what happens when count reaches 0?
Why: When count is 0 there is no active guess, so the current number becomes the new candidate.
- 4
What allows the voting algorithm to work correctly?
Why: Because the majority value appears more than n/2 times, it outnumbers all others combined and is always left standing after the cancelling.