Objective
An element is a majority if it appears in more than half of the positions. The Boyer-Moore algorithm finds it by going through the list only once and using very little memory.
Instructions
- Read a line with integers separated by spaces.
- Keep a
candidate(initially none) and a countercountat 0. For each number: if the counter is 0, the number becomes the candidate and the counter is 1; if it equals the candidate, add 1; if it differs, subtract 1. - At the end check (with
list.count) that the candidate appears in more than half of the elements. - Show
Majority: xor, if there is none,No majority.
2 2 1 1 2 2 3
Majority: 2
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise