AlgoMaster Logo

Majority Element

Last Updated: March 19, 2026

easy
4 min read

Understanding the Problem

We need to find the element that appears more than half the time in the array. The problem guarantees such an element exists, which is an important detail. It means we don't need to verify our answer, we just need to find it.

Think about what "more than n/2 times" really means. If you lined up all the elements, the majority element takes up more than half the line. That's a powerful property. It means no other element can also appear more than n/2 times. There's always exactly one majority element. This observation opens the door to some clever algorithms beyond the obvious counting approach.

Approach 1: Hash Map

Intuition

The most natural first thought: just count everything. If we know how many times each element appears, we can simply return the one whose count exceeds n/2.

A hash map is perfect for this. We iterate through the array once, incrementing the count for each element. Then we look for the element whose count is greater than n/2. We can even check as we go and return early the moment we find an element that crosses the threshold.

Algorithm

  1. Create a hash map to store the count of each element.
  2. Iterate through the array. For each element, increment its count in the map.
  3. If the count for the current element exceeds n / 2, return that element immediately.
  4. If the loop finishes without returning (won't happen given the guarantee), return -1 as a fallback.

Example Walkthrough

1Initialize: threshold = 7/2 = 3, counts = {}
0
2
i
1
2
2
1
3
1
4
1
5
2
6
2
1/8

Code

This approach works correctly but uses extra space.

Can we find the majority element without remembering the count of every element? What if we exploited the fact that the majority element appears more than all others combined?

Approach 2: Sorting

Intuition

Here's a neat observation: if you sort the array, the majority element must be sitting at index n/2.

Why? Because the majority element takes up more than half the array. No matter how you arrange it, after sorting, it has to "straddle" the middle position.

Think about it this way. If the majority element is the smallest value, it occupies indices 0 through at least n/2. If it's the largest, it occupies indices starting from at most n/2 down to n-1. In every case, index n/2 is covered.

So we just sort and grab the middle element. No counting needed.

Algorithm

  1. Sort the array.
  2. Return the element at index n / 2.

Example Walkthrough

1Original array, unsorted
0
2
1
2
2
1
3
1
4
1
5
2
6
2
1/6

Code

Sorting takes O(n log n) time. We're rearranging the entire array just to read one element at the midpoint. We don't need the array to be fully sorted. That's a lot of unnecessary ordering. We only need to know which element sits at position n/2.

Is there a way to find the majority element in a single pass, without sorting or extra storage? What if we used the "more than half" property to cancel out non-majority elements?

Approach 3: Boyer-Moore Voting Algorithm

Intuition

This is one of those algorithms that seems almost too simple to be correct. The idea: walk through the array maintaining a "candidate" and a counter. When you see the candidate, increment the counter. When you see something else, decrement it. If the counter hits zero, pick the current element as the new candidate and reset the counter to 1.

Algorithm

  1. Initialize candidate to any value and count to 0.
  2. Iterate through each element in the array:
    • If count is 0, set the current element as the new candidate and set count to 1.
    • Otherwise, if the current element equals candidate, increment count.
    • Otherwise, decrement count.
  3. Return candidate.

Example Walkthrough:

1Initialize: candidate=?, count=0
0
2
i
1
2
2
1
3
1
4
1
5
2
6
2
1/8

Code

The key invariant is this: at any point during the traversal, the majority element among the remaining unprocessed elements is the same as the majority element of the entire array. When the count drops to zero, the current candidate has been "cancelled out" by an equal number of different elements. Since we've removed equal counts of the candidate and non-candidates, the majority element in the remaining portion is still the overall majority.

More formally, if the majority element appears m times (m > n/2), the maximum number of times it can be "cancelled" is n - m (the count of all other elements). Since m > n - m, the majority element's net count is always positive. So it will be the candidate when the algorithm finishes.