Majority Element II

medium

You are given an integer array nums of length n. Find every element that appears strictly more than ⌊n / 3⌋ times.

Return the qualifying elements in ascending order. If no element clears the bar, return an empty array.

Note the threshold carefully: ⌊n / 3⌋ uses integer division, and the count must be strictly greater. For n = 8 the threshold is 2, so an element needs at least 3 appearances to qualify — and as you'll discover, no more than two elements can ever qualify at once.

Hints

How many different values could each appear more than a third of the time? Try to build an array containing three of them.
At most two — three values each appearing more than n/3 times would together need more than n elements. So you are hunting for at most two candidates, then checking them.
Extend the Boyer–Moore voting idea to two candidate seats with counters: a matching vote strengthens a seat, an outsider cancels one vote from both, and a final recount confirms which survivors truly exceed ⌊n/3⌋.

Common doubts

Pigeonhole: if three values each appeared more than n/3 times, their combined count would exceed n — more elements than the array holds. So the answer always has size 0, 1, or 2.
Yes. In [1,2,3] the threshold is ⌊3/3⌋ = 1 and every value appears exactly once, so nothing qualifies — return an empty array.
The scan guarantees no true winner is eliminated, but a value without enough votes can still occupy a seat at the end (try [1,2,3,4]). Recounting the survivors separates real winners from lucky pretenders.
Yes — ascending order. Since the result holds at most two values, sorting it costs essentially nothing.

Interview follow-ups

Keep k − 1 candidate seats with counters (the Misra–Gries algorithm): a match increments its seat, an empty seat gets claimed, otherwise all k − 1 counters decrement — a k-way cancellation. Verify the survivors with a recount. Time O(n·k) naively or O(n) with a small hash of the candidates, space O(k).
The two counters update online in one pass, but the recount needs the data again. A pure single pass yields a small superset of the heavy hitters — exact confirmation requires a second pass or stored counts, which is precisely how streaming heavy-hitter detectors are deployed.
Any value occupying more than a third of a sorted array must cover index ⌊n/3⌋ or ⌊2n/3⌋. Check just those two candidates, counting each with binary search over its first and last occurrence — O(log n) total.

Fun facts

  • The voting trick comes from Robert Boyer and J Strother Moore, who devised the original majority-vote algorithm in 1981 — they described it as dissenting voters pairing off and knocking each other out until only one bloc remains standing.
  • The two-seat version is a special case of the Misra–Gries heavy-hitters algorithm (1982), which network routers and analytics pipelines still use to spot the most frequent items in massive data streams without storing them.

Asked at

AmazonGoogleMicrosoftBloombergAdobe
Frequently Sometimes Occasionally
Example 1
Input: nums = [3,2,3]
Output: [3]
n = 3, so the threshold is ⌊3/3⌋ = 1. The value 3 appears 2 times — strictly more than 1.
Example 2
Input: nums = [1]
Output: [1]
n = 1, so the threshold is ⌊1/3⌋ = 0. The value 1 appears once — strictly more than 0.
Example 3
Input: nums = [1,2]
Output: [1,2]
n = 2, so the threshold is 0. Both values appear once, and 1 > 0 — both qualify.
Constraints

- 1 <= nums.length <= 5 * 10^4 - -10^9 <= nums[i] <= 10^9

Solve this problem →