Skip to content

Majority Element

Given an array nums of size n, return the majority element.

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

Example 1:

Input: nums = [3,2,3]
Output: 3

Example 2:

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

Constraints:

  • n == nums.length
  • 1≤n≤5×1041 \le n \le 5 \times 10^4
  • −109≤-10^9 \le nums[i] ≤109\le 10^9
  • The input is generated such that a majority element will exist in the array.

Follow-up: Could you solve the problem in linear time and in O(1) space?

Sorting and returning nums[n / 2] works in O(nlog⁡n)O(n \log n); a hash map of counts works in O(n)O(n) time but O(n)O(n) space. The follow-up asks for both linear time and constant space.

Think of it as an election where every element votes for itself, and pair off votes for different values so that they cancel. Each cancellation removes one copy of two different values, so it removes at most one copy of the majority element and at least one copy of something else. The majority element has more copies than all other values combined, so it cannot be cancelled away completely: whatever survives at the end must be the majority.

The Boyer-Moore vote does this cancellation in one scan with a single candidate and a counter count:

  • if count == 0, the current element becomes the new candidate;
  • an element equal to candidate adds a vote (count++), any other element cancels one (count--).
  1. Set count = 0.
  2. For each x in nums: if count == 0, set candidate = x. Then count += (x == candidate) ? 1 : -1.
  3. Return candidate. (No verification pass is needed because the majority element is guaranteed to exist.)

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

x2211122
candidate2222112
count1210101

The 1s briefly take over, but the extra 2s cancel them, and 2 is the survivor.

  • Time: O(n)O(n) — a single pass.
  • Space: O(1)O(1) — two integers.
  • n = 1: the only element becomes the candidate and is returned.
  • Majority element appears late (e.g. [1,2,3,3,3]): earlier candidates are cancelled to count = 0, and the majority takes over before the end.

Think before you peek

Work out an approach on your own first — the explanation will still be here when you want it.

class Solution {
public:
int majorityElement(vector<int>& nums) {
int candidate = 0, count = 0;
for (int x : nums) {
if (count == 0) candidate = x;
count += (x == candidate) ? 1 : -1;
}
return candidate;
}
};
class Solution {
public int majorityElement(int[] nums) {
int candidate = 0, count = 0;
for (int x : nums) {
if (count == 0) candidate = x;
count += (x == candidate) ? 1 : -1;
}
return candidate;
}
}
class Solution:
def majorityElement(self, nums: list[int]) -> int:
candidate, count = 0, 0
for x in nums:
if count == 0:
candidate = x
count += 1 if x == candidate else -1
return candidate

Give it a try first

Attempt the problem on LeetCode — the code will still be here.