Majority Element
Boyer-Moore Voting: the majority element survives the vote cancellation.
Problem Statement
Given an array nums of size n, return the majority element — the element that appears more than ⌊n/2⌋ times. You may assume the majority element always exists.
Examples
Example 1
Example 2
Constraints
- n == nums.length
- 1 ≤ n ≤ 5 × 10⁴
- −2³¹ ≤ nums[i] ≤ 2³¹ − 1
- Majority element always exists.
Solutions
Count each element's frequency. Return the one that appears > n/2 times. O(n) time and space.
Related Concepts
Deepen your understanding with these related topics from our AI Glossary:
Want to master the core concepts?
Our free AI Glossary covers 190+ topics — from Boyer-Moore Voting to Dynamic Programming, Machine Learning, SQL, and more. Structured learning tracks for every level.
Stuck? Ask AI to explain it step by step.
Ask Claude, GPT-4o, or Gemini to debug your code, generate test cases, or walk through the intuition. 39+ models. Pay only on days you use it — no subscription required.
Free to start · No credit card required to explore
Get Started Free