🚀 Phases 1–5 are live — Days 1–17 cover the foundations and the algorithmic patterns. See the roadmap →

Majority Element easy

Description

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

> Case 1:
    nums = [3, 2, 3]
    Output: 3
 
> Case 2:
    nums = [2, 2, 1, 1, 1, 2, 2]
    Output: 2

Constraints

  • n == nums.length
  • 1 <= n <= 5 × 10^4
  • -10^9 <= nums[i] <= 10^9

Follow-up: solve in O(n) time and O(1) extra space.

Solution 1 — Divide and Conquer

The recursive observation: if x is the majority element of the whole array, then x is the majority element of the left half, or the right half, or both.

So:

  • Divide: halve the array.
  • Conquer: recursively find the majority of each half.
  • Combine: if both halves agree on a candidate, return it. Otherwise count both candidates in the full range and return whichever wins.

Recurrence: T(n) = 2 T(n / 2) + O(n)O(n log n).

Time: O(n log n). Space: O(log n).

Solution 2 — Boyer-Moore Voting (the “real” answer)

A genius O(n) time, O(1) space algorithm. Maintain a candidate and a counter:

  • If counter is 0, set the candidate to the current element.
  • Otherwise, increment counter if the current element matches the candidate; decrement if not.

After the pass, the candidate is the majority.

Why it works: every time the counter decrements, we’re “cancelling out” one non-majority vote against one majority vote. Since the majority element appears more than n / 2 times, there are too many of its votes to be fully cancelled — it survives as the candidate at the end.

Solution 3 — Sort + Pick the Middle

def majority_element(nums):
    nums.sort()
    return nums[len(nums) // 2]

O(n log n) time, O(1) extra space. If the majority element appears more than n/2 times, after sorting it must occupy the middle position. The cleanest one-liner — but doesn’t generalize when the majority guarantee is weaker.

Analysis comparison

ApproachTimeSpaceNotes
Divide & ConquerO(n log n)O(log n)Pedagogically clean; not the optimal answer
Boyer-Moore votingO(n)O(1)The “real” answer; usually expected
Sort + middleO(n log n)O(1)One line; works because majority is > n/2
Hash map countO(n)O(n)Lazy fallback; works for non-majority variants

Same skin

  • Majority Element II (appears more than n/3 times) — extended Boyer-Moore with two candidates.
  • Find Two Numbers with Frequencies above n/3 — multi-candidate variant.
  • Find the Duplicate Number — different shape but similar “single special element” feel.