Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Majority Element
Problem
You are given an integer array nums that is guaranteed to contain a majority element: a value that occurs strictly more than len(nums) / 2 times. Return that value.
- The array is non-empty and has at most 100,000 elements.
- Values are arbitrary integers, positive or negative.
- A majority element always exists (the algorithm below depends on this guarantee).
Examples
nums = [8, 5, 8]→8. It appears 2 of 3 times.nums = [4, 7, 7, 4, 7, 7, 4, 7, 7]→7. Six of nine.nums = [-6]→-6. A single element is trivially the majority.
Hints
- Counting with a hash map works in O(n) time and O(n) space; can you drop the space?
- Sorting puts the majority element at the middle index, but that costs O(n log n).
- Pair off each element with a different element and cancel both; the majority survives. This is Boyer-Moore majority voting.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.