AlgoMaster Logo

Majority Element II

mediumFrequency6 min readUpdated June 23, 2026

Understanding the Problem

This is a generalization of the classic Majority Element problem (LeetCode 169), where we needed to find the element appearing more than n/2 times. That problem guaranteed exactly one answer. Here, we need to find all elements appearing more than n/3 times.

A mathematical constraint bounds the answer: at most two elements can appear more than n/3 times. If three elements each appeared more than n/3 times, that would account for more than n total elements, which is impossible. So the answer list has at most 2 elements.

This bound shapes every approach. The hash map counts every element and filters, sorting groups equal values into runs, and the Boyer-Moore approach exploits the "at most 2 candidates" fact to run in constant space.

Key Constraints:

  • 1 <= nums.length <= 5 * 10^4 → With n up to 50,000, O(n^2) means 2.5 billion operations, which is too slow. We need O(n log n) or better.
  • -10^9 <= nums[i] <= 10^9 → Values span the full 32-bit integer range, so we can't use array indexing for counting. We need a hash map or a comparison-based approach.
  • The follow-up explicitly asks for O(n) time and O(1) space, hinting at the Boyer-Moore Voting algorithm.

Approach 1: Hash Map Counting

Intuition

Count how many times each element appears, then check which counts exceed n/3. A hash map gives O(1) average lookups and insertions, so the counting takes a single pass, and a second pass over the distinct keys filters out the elements above the threshold.

Algorithm

  1. Create a hash map to store the count of each element.
  2. Iterate through nums, incrementing the count for each element.
  3. Calculate the threshold as n / 3.
  4. Iterate through the hash map entries, collecting elements whose count exceeds the threshold.
  5. Return the result list.

Example Walkthrough

1Initialize: scan nums to count each element
0
3
i
1
2
2
3
1/6

Code

This is correct but uses O(n) extra space for the count map. The next approach removes that auxiliary space at the cost of a slower runtime.

Approach 2: Sorting

Intuition

After sorting, all occurrences of the same element become adjacent. An element that appears more than n/3 times occupies a contiguous run longer than n/3. So one scan over the sorted array, measuring each run's length and comparing it against the threshold, finds every majority element.

This trades the O(n) space of a hash map for the O(n log n) time of sorting, and it modifies the input.

Algorithm

  1. Sort the array.
  2. Initialize a counter and iterate through the sorted array.
  3. For each new value, count its consecutive occurrences.
  4. If the count exceeds n/3, add it to the result.
  5. Return the result.

Example Walkthrough

1Before sorting: nums = [3, 2, 3, 1, 2, 3, 2]
0
3
1
2
2
3
3
1
4
2
5
3
6
2
1/6

Code

Sorting still costs O(n log n). The final approach finds the candidates in linear time and constant space, satisfying the follow-up.

Approach 3: Boyer-Moore Voting (Optimal)

Intuition

This extends the Boyer-Moore Majority Vote algorithm. The original finds the element appearing more than n/2 times using one candidate and one counter. For the n/3 threshold, it uses two candidates and two counters.

The mechanism is cancellation. When an element matches neither candidate and both counters are non-zero, the algorithm decrements both counters, discarding one occurrence of each candidate along with the current element. Three distinct elements remove each other from contention. After the first pass, the two surviving candidates are the only possible answers, but survival alone does not prove either exceeds n/3, so a second pass counts their true frequencies.

Algorithm

  1. Initialize two candidates (cand1, cand2) and their counts (count1 = 0, count2 = 0).
  2. First pass (find candidates): iterate through nums:
    • If num == cand1, increment count1.
    • Else if num == cand2, increment count2.
    • Else if count1 == 0, set cand1 = num and count1 = 1.
    • Else if count2 == 0, set cand2 = num and count2 = 1.
    • Else decrement both count1 and count2.
  3. Second pass (verify candidates): count the actual occurrences of cand1 and cand2.
  4. Add to result any candidate whose count exceeds n/3.

Example Walkthrough

1Initialize: cand1=?, count1=0, cand2=?, count2=0
0
1
i
1
2
2
3
3
1
4
1
5
2
6
2
1/7

Code