Problem
Section titled “Problem”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: 3Example 2:
Input: nums = [2,2,1,1,1,2,2]Output: 2Constraints:
n == nums.length-
nums[i] - 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?
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Sorting and returning nums[n / 2] works in ; a hash map of counts works in time but 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 newcandidate; - an element equal to
candidateadds a vote (count++), any other element cancels one (count--).
Algorithm
Section titled “Algorithm”- Set
count = 0. - For each
xinnums: ifcount == 0, setcandidate = x. Thencount += (x == candidate) ? 1 : -1. - Return
candidate. (No verification pass is needed because the majority element is guaranteed to exist.)
Walkthrough
Section titled “Walkthrough”Example 2, nums = [2,2,1,1,1,2,2]:
x | 2 | 2 | 1 | 1 | 1 | 2 | 2 |
|---|---|---|---|---|---|---|---|
candidate | 2 | 2 | 2 | 2 | 1 | 1 | 2 |
count | 1 | 2 | 1 | 0 | 1 | 0 | 1 |
The 1s briefly take over, but the extra 2s cancel them, and 2 is the survivor.
Complexity
Section titled “Complexity”- Time: — a single pass.
- Space: — two integers.
Edge cases
Section titled “Edge cases”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 tocount = 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 candidateGive it a try first
Attempt the problem on LeetCode — the code will still be here.