Browse curriculum

Majority Element

Find the value that appears more than n/2 times: count with a hash map, or use Boyer-Moore voting for O(n) time and O(1) space.

Problem Understanding

Majority Element: given an array nums of size n, return the element that appears more than ⌊n / 2⌋ times. The majority element always exists.

Example: [2, 2, 1, 1, 1, 2, 2] returns 2 (4 of 7 values).

The Intuition: Count, Then Cancel

Hash map first. The direct answer is a hash map of counts, the same counting used in Valid Anagram and Top K Frequent Elements. Return the value whose count passes n / 2. O(n) time, O(n) space.

Boyer-Moore voting. The follow-up asks for O(1) space. Picture every value voting: copies of the same value support each other, and two different values cancel each other out.

  1. Keep a candidate and a count.
  2. When count is 0, the current value becomes the candidate.
  3. A matching value adds a vote; any other value removes one.

The majority has more copies than all other values combined, so it cannot be cancelled away: it is the candidate at the end.

Interactive Walkthrough

nums is on top with the current value filled; below it, the candidate's votes as tokens. Try Late majority to watch the candidate change several times before the real majority takes over.

Dry Run Table

Input: nums = [2, 2, 1, 1, 1, 2, 2]

Value Action Candidate Count
2 count 0: pick 2 1
2 match 2 2
1 cancel 2 1
1 cancel 2 0
1 count 0: pick 1 1
2 cancel 1 0
2 count 0: pick 2 1

Answer: 2.

The Solution Template

Boyer-Moore voting: one candidate, one count, one pass.

Majority Element Code
1var majorityElement = function(nums) {
2 let candidate = 0, count = 0;
3 for (const x of nums) {
4 if (count === 0) candidate = x;
5 count += x === candidate ? 1 : -1;
6 }
7 return candidate;
8};

Edge Cases & Common Mistakes

  • One element: it is the majority; the loop picks it immediately.
  • The candidate changes: that is expected. Intermediate candidates can be wrong; only the final one is guaranteed.
  • No guaranteed majority: in variants like Majority Element II or when the input may lack one, verify the candidate with a second counting pass.
  • Sorting: returning sorted(nums)[n // 2] also works, but costs O(n log n).
  • "At least n/2" vs "more than n/2": the guarantee needs strictly more than half; with exactly half, voting can end on the other value.

Summary

A hash map of counts finds the majority in O(n) time and O(n) space. Boyer-Moore voting does it in O(1) space: keep a candidate and a count, take a new candidate when the count hits 0, add for matches and subtract otherwise. The candidate at the end is the majority.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Start with no candidate

    Set count = 0.

  2. Vote

    For each value: if count is 0, make it the candidate. Then add 1 if it equals the candidate, otherwise subtract 1.

  3. Return the candidate

    The candidate left at the end is the majority element.

Frequently asked questions

How does the Boyer-Moore voting algorithm work?

Keep a candidate and a count. When the count is 0, take the current value as the candidate. Add 1 for a matching value and subtract 1 otherwise. Each subtraction cancels one copy of the candidate against one other value; the majority has more copies than everything else combined, so it is the candidate at the end.


What is the hash map solution for Majority Element?

Count every value in a hash map and return the one whose count exceeds n/2, which you can do the moment it crosses that line. It is O(n) time but O(n) space; Boyer-Moore drops the space to O(1).


Does Boyer-Moore work if there might be no majority?

It always returns a candidate, but that candidate is only guaranteed to be the majority if one exists. If the input might have none, make a second pass to count the candidate and check it exceeds n/2.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description