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.
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.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.
nums, incrementing the count for each element.n / 3.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.
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.
n/3, add it to the result.Sorting still costs O(n log n). The final approach finds the candidates in linear time and constant space, satisfying the follow-up.
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.
Each cancellation step removes three distinct values at once: the current element and one tallied occurrence of each candidate. An element that appears count times can therefore lose at most one tallied occurrence per cancellation, and each cancellation also consumes a value not equal to it. The most cancellations possible is (n - count) / 2, since every cancellation pairs the element against two other distinct values. When count > n/3, that bound is below count, so the element keeps a positive tally and ends as one of the two candidates.
The converse does not hold. A candidate can survive the first pass without appearing more than n/3 times, for example when no true majority element exists. The second pass that recounts both candidates is therefore required.
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.