Skip to content
AI360Xpert
Beta
Difficulty: EasyArrays & Hashing

Majority Element

Problem in Plain English

Given an array of size n where one element appears more than n divided by 2 times, return that majority element. The input guarantees such an element always exists.

Problem Statement

Given an integer array nums of length n, return the value that occurs strictly more than half the time. Such a majority element is guaranteed to exist.

A majority needs at least floor(n / 2) + 1 occurrences; being the most frequent value alone is insufficient.

Follow-up: find it in O(n)O(n) time with O(1)O(1) extra space.

Constraints

  • n == nums.length
  • 1 <= n <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9
  • A majority element always exists.

Examples

Example 1

Input
nums = [3, 2, 3]
Output
3

3 appears twice, which is more than 3 / 2 = 1.5, so 3 is the majority element.

Example 2

Input
nums = [2, 2, 1, 1, 1, 2, 2]
Output
2

2 appears four times, which is more than 7 / 2 = 3.5, so 2 is the majority element.

Example 3

Input
nums = [-1]
Output
-1

The only value occurs once, more than 1 / 2. A negative majority is a valid answer, so -1 must not be treated as a missing-answer marker.

Intuition

Imagine removing two different values together until no such pair remains. Each pair consumes at most one occurrence of the majority, and there are fewer other values than majority occurrences, so some majority occurrences must survive. In [3, 2, 3], cancelling the first 3 with 2 leaves 3. Boyer-Moore voting simulates these removals with a candidate value and a count of its unmatched occurrences; that count is not the candidate's total frequency.

Approaches

Brute Force

Solution Details

Reveal Brute Force: intuition, complexity, and code

Hints

Hint 1
Counting every candidate separately works but rescans the array each time; consider what that costs at 5 * 10^4 elements.
Hint 2
A sorted array would place the majority at the middle index, which hints that the majority outnumbers the rest combined.
Hint 3
Keep one candidate plus a counter: adopt a new candidate at zero, then add or subtract one vote per value.

Edge Cases

  • Single value: [-1] returns -1; zero and negative candidates are ordinary values, not special markers.
  • All equal: [7, 7, 7, 7] returns 7, and the voting count increases on every element.
  • Minimum even-length majority: [1, 2, 2, 2] returns 2; two copies would only tie half the length.
  • Several resets: [1, 2, 3, 3, 3] cancels 1 with 2, then adopts the later majority 3.
  • Extreme integers: [-1000000000, 1000000000, -1000000000] returns -1000000000; the algorithm compares values without subtracting them.
  • Without the guarantee: [1, 2, 3] leaves a candidate but has no majority. That variant requires a frequency verification pass; empty arrays are outside this problem's constraints.

Common Mistakes and Interview Tips

  • Using count >= floor(n / 2) in brute force admits a tie; use a strict greater-than comparison.
  • Treating the voting count as a frequency: Example 1 finishes at 1 although the majority 3 occurs twice.
  • Counting a mismatching value again as a new vote immediately after it brings count to zero. It already cancelled a vote; the next value starts the fresh candidacy.
  • Adopting a candidate but failing to count the current element as its first vote. Set the candidate before the equality-based update.
  • Interview tip: state the majority guarantee before omitting verification, and explain the cancellation invariant rather than assuming the last candidate is always valid.

Key Takeaway

When one value is guaranteed to outnumber all others combined, pairwise cancellation finds it in linear time with constant space.