Majority Element II
viaLeetCode
Problem: Given an integer array of size n, find all elements that appear more than ⌊n/3⌋ times.
Constraints: Array length up to 5 * 10^4.
Example: [3,2,3] -> [3]. [1,1,1,3,3,2,2,2] -> [1,2].
Approach: Extended Boyer-Moore Voting Algorithm: since at most 2 elements can appear more than n/3 times, maintain two candidates with two counters, updating them via the standard Boyer-Moore rules; a second pass verifies actual counts of the two candidates exceed n/3. O(n) time, O(1) space.
asked …