23. Majority Element
easyAsked at RedditFind the element that appears more than n/2 times in an array. Reddit uses Boyer-Moore voting to test whether candidates recognize specialized algorithms — the same vote-majority pattern matters for their vote-fraud detection (one IP shouldn't dominate a thread).
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit trust-and-safety phone screen — followed by vote-fraud detection follow-ups.
- Blind (2025-10)— Reported as a Reddit fraud-team favorite.
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. Follow-up: Could you solve the problem in linear time and in O(1) space?
Constraints
n == nums.length1 <= n <= 5 * 10^4-10^9 <= nums[i] <= 10^9
Examples
Example 1
nums = [3,2,3]3Example 2
nums = [2,2,1,1,1,2,2]2Approaches
1. Hash count
Count each element. Return the one with count > n/2.
- Time
- O(n)
- Space
- O(n)
function majorityElement(nums) {
const count = new Map();
for (const n of nums) {
count.set(n, (count.get(n) || 0) + 1);
if (count.get(n) > nums.length / 2) return n;
}
}Tradeoff: Linear time but O(n) space — fails the follow-up.
2. Boyer-Moore voting (optimal)
Single pass with a candidate and count. On match, count++. On mismatch, count--. When count hits 0, switch candidate.
- Time
- O(n)
- Space
- O(1)
function majorityElement(nums) {
let candidate = null, count = 0;
for (const n of nums) {
if (count === 0) candidate = n;
count += (n === candidate) ? 1 : -1;
}
return candidate;
}Tradeoff: O(1) space. Beautiful algorithm — works because a true majority survives all cancellations.
Reddit-specific tips
Reddit interviewers want Boyer-Moore named explicitly. Bonus signal: connect this to their bot-detection — if a single user/IP appears in majority of votes on a post, the vote is likely brigaded. The intuition is the same: a single dominant signal survives cancellations.
Common mistakes
- Comparing count > n/2 inside the loop (only valid if majority is guaranteed).
- Forgetting to reassign candidate when count hits 0.
- Initializing count to 1 (must start at 0 so the first element becomes candidate).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Majority element II (LC 229): elements appearing > n/3 times. Two candidates.
- What if no guarantee of majority? Verify with a second pass.
- Distributed majority — combining counts across shards.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does Boyer-Moore work?
Pair each majority element with a non-majority one. Since majority is > n/2, there's always one unpaired — the survivor.
What if majority isn't guaranteed?
Run a second pass to verify the candidate's count is actually > n/2.
Practice these live with InterviewChamp.AI
Drill Majority Element and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →