Skip to content

Majority Element

Given an array nums of size n, return the majority element — the element that appears more than n / 2 times.

You may assume that the majority element always exists in the array.

InputOutputExplanation
[3, 2, 3]33 appears 2 times out of 3 (> 1.5)
[2, 2, 1, 1, 1, 2, 2]22 appears 4 times out of 7 (> 3.5)
  • n == nums.length
  • 1 <= n <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9
  • A majority element always exists.
ApproachTimeSpaceBest When
Hash MapO(n)O(n)Straightforward to understand; easy to adapt to count all elements
Boyer-MooreO(n)O(1)Memory is critical; the classic interview-worthy approach
  • Learn Hash Map first to build intuition — count everything, return the one that exceeds the threshold.
  • Learn Boyer-Moore for interviews — O(1) space is the follow-up every interviewer expects.
Interview Favourite
Boyer-Moore
O(n) time · O(1) space
Easy to Understand
Hash Map
O(n) time · O(n) space
✓ Simple

Count every element’s frequency in a hash map, then return the first element whose count exceeds n / 2. Because the majority element is guaranteed to exist, the loop always finds one.

⏱ Time O(n) Single pass 💾 Space O(n) Map stores up to n elements

Input: [2, 2, 1, 1, 1, 2, 2], threshold = 3

numcount after> threshold?
2{2:1}No
2{2:2}No
1{2:2, 1:1}No
1{2:2, 1:2}No
1{2:2, 1:3}No
2{2:3, 1:3}No
2{2:4, 1:3}Yes → return 2
function majorityElement(nums):
threshold = len(nums) // 2
counts = {}
for num in nums:
counts[num] = counts.get(num, 0) + 1
if counts[num] > threshold:
return num
majority_element_hash_map.py
from typing import List
def majority_element(nums: List[int]) -> int:
counts = {}
threshold = len(nums) // 2
for num in nums:
counts[num] = counts.get(num, 0) + 1
if counts[num] > threshold:
return num
return nums[0]
🎯 Interview Favourite

Maintain a candidate and a count. When count drops to zero, the current element becomes the new candidate. When the current element matches the candidate, increment; when it differs, decrement. The majority element’s count can never be fully cancelled out, so it always survives as the final candidate.

Mental model: think of it as a battle. Every non-candidate element cancels one candidate vote. Because the majority element has more votes than all others combined, it wins the war even if it loses individual battles.

⏱ Time O(n) Single pass 💾 Space O(1) Two variables only

Input: [3, 2, 3]

inumActioncandidatecount
03Initialise31
122 ≠ 3, decrement30
23count=0, new candidate31
Return 3

Input: [2, 2, 1, 1, 1, 2, 2]

inumcandidatecount
0221
1222
2121
3120
4111
5210
6221
Return 2

Animated walkthrough

How Boyer-Moore voting finds the majority element

Use Next or Autoplay to watch the candidate and count change as each element either reinforces or challenges the current front-runner.

Step 1 of 3
Waiting to begin...
Array state
Memory / lookup state

function majorityElement(nums):
candidate, count = nums[0], 1
for num in nums[1:]:
if count == 0:
candidate, count = num, 1
elif num == candidate:
count += 1
else:
count -= 1
return candidate
majority_element_boyer_moore.py
from typing import List
def majority_element(nums: List[int]) -> int:
candidate, count = nums[0], 1
for num in nums[1:]:
if count == 0:
candidate, count = num, 1
elif num == candidate:
count += 1
else:
count -= 1
return candidate
print(majority_element([3, 2, 3])) # 3
print(majority_element([2, 2, 1, 1, 1, 2, 2])) # 2
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
majority_element_space_optimized.py
"""
Solution for Majority Element
"""
def solve():
"""Implementation for approach 3 of Majority Element"""
pass
if __name__ == "__main__":
print("Solution ready")