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.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account