Last Updated: March 31, 2026
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.
The first thing to notice is a mathematical constraint: there can be at most two elements that appear more than n/3 times. Think about it. If three elements each appeared more than n/3 times, that would account for more than n total elements, which is impossible. So our answer list has at most 2 elements.
This observation is the foundation for every approach. The brute force counts every element, the hash map approach does it efficiently, and the optimal Boyer-Moore approach exploits this "at most 2 candidates" fact to use constant space.
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 most natural approach: count how many times each element appears, then check which counts exceed n/3. A hash map gives us O(1) lookups and insertions, so we can count everything in a single pass and then filter.
This is the approach most people would reach for in a real codebase. It's clean, readable, and hard to get wrong.
nums, incrementing the count for each element.n / 3.This approach works correctly but uses O(n) extra space. Since at most 2 elements can be the answer, can we find those candidates without storing counts for every element?
If we sort the array, all occurrences of the same element will be adjacent. An element that appears more than n/3 times will occupy a contiguous block of length greater than n/3. So after sorting, we can scan the array and check whether each element's run length exceeds the threshold.
This approach trades the O(n) space of a hash map for the O(n log n) time of sorting. It's a reasonable middle ground if you want constant auxiliary space and can afford the slower runtime.
n/3, add it to the result.Sorting works but takes O(n log n) and modifies the input. Since at most 2 elements can be the answer, can we find those candidates in a single pass without sorting or extra space?
This is the extended version of the Boyer-Moore Majority Vote algorithm. The original algorithm finds the element appearing more than n/2 times using one candidate and one counter. For n/3, we extend it to two candidates and two counters.
The core idea is a cancellation mechanism. If three people all vote for different candidates, their votes cancel out. After all cancellations, whoever remains standing must be a potential majority candidate. But "potential" is the key word. We still need a second pass to verify.
If an element truly appears more than n/3 times, its count can be decremented at most (n - count) / 2 times (since each cancellation requires two other distinct elements). Since count > n/3, the number of cancellations is at most (n - n/3) / 2 = n/3, which is less than the element's count. So it will survive the first pass.
But the converse isn't true. An element can survive without being a majority element. That's why the second verification pass is mandatory.
cand1, cand2) and their counts (count1 = 0, count2 = 0).nums:num == cand1, increment count1.num == cand2, increment count2.count1 == 0, set cand1 = num and count1 = 1.count2 == 0, set cand2 = num and count2 = 1.count1 and count2.cand1 and cand2.n/3.