Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use Boyer-Moore Voting: maintain a candidate and a count. When count drops to zero, adopt the current element as the new candidate. Increment count for matches, decrement for mismatches. The majority element always survives as the final candidate.
The majority element appears more than n/2 times. Every time it is "cancelled" by a different element, at most one copy is lost. Since it has more copies than all others combined, it always remains as the last candidate standing.
Updated Feb 2026