You are on page 1of 2

BoyerMoore majority vote algorithm

2 Implementations

The Boyer-Moore Vote Algorithm solves the majority


vote problem in linear time O(n) and logarithmic space
O(log n) . The majority vote problem is to determine in
any given sequence of choices whether there is a choice
with more occurrences than half of the total number
of choices in the sequence and if so, to determine this
choice. Note how this denition contrasts with the mode
in which it is not simply the choice with the most occurrences, but the number of occurrences relative to the total
length of the sequence.

2.1 Java

1 import java.util.*; 2 public class MajorityVote {


3 public int majorityElement(int[] num) { 4 int n =
num.length; 5 int candidate = num[0], counter = 0; 6 for
(int i : num) { 7 if (counter == 0) { 8 candidate = i; 9
counter = 1; 10 } else if (candidate == i) { 11 counter++;
12 } else { 13 counter--; 14 } 15 } 16 17 counter = 0;
Mathematically, given a nite sequence (length n) of 18 for (int i : num) { 19 if (i == candidate) counter++;
numbers, the object is to nd the majority number de- 20 } 21 if (counter <= n / 2) return 1; 22 return
ned as the number that appears more than n/2 times. candidate; 23 24 } 25 public static void main(String[]
args) { 26 MajorityVote s = new MajorityVote(); 27
System.out.format("%d\n,
s.majorityElement(new
int[]
{1,
2,
3}));
28
System.out.format("%d\n,
1 Description
s.majorityElement(new int[] {2, 2, 3})); 29 } 30 }
The algorithm is carried out in two steps:

2.2 JavaScript

1. Eliminate all elements except one.


Iterating through the array of numbers, maintain a current candidate and a
counter initialized to 0. With the current element x in iteration, update the
counter and (possibly) the candidate: if
the counter is 0, set the current candidate to x and the counter to 1. If the
counter is not 0, increment or decrement
the counter based on whether x is the
current candidate.

1 function majorityVote(nums) { 2 var n = nums.length;


3 var candidate = nums[0]; 4 var counter = 0; 5 6
nums.forEach(function(num) { 7 if (counter === 0) { 8
candidate = num; 9 counter = 1; 10 } else if (candidate
=== num) { 11 counter++; 12 } else { 13 counter--; 14 }
15 }); 16 17 // count the number of times the candidate
appears 18 counter = 0; 19 nums.forEach(function(num)
{ 20 if (num === candidate) { 21 counter++; 22 } 23
}); 24 if (counter <= n / 2) { 25 return 1; 26 } 27
return candidate; 28 } 29 30 function test(nums) { 31
2. Determine if the remaining element is a valid ma- console.log('for', nums, 'result', majorityVote(nums));
32 } 33 34 // equal number of occurrences for all for
jority element.
[1, 2, 3] result 1 35 test([1,2,3]); 36 37 // 2 a clear
majority. outputs 2 38 test([1,2,2,2,2,2,4]); 39 40 //
With the candidate acquired in step 1, ithere, 2 has the highest number of occurrences (mode)
erate through the array of numbers and
but not the majority 41 // (aka half the length of the
count its occurrences. Determine if the
array). outputs 1 42 test([2,1,1,2,3,3,2,4,4,2,5,5]);
result is more than half of the sequences
length. If so, the candidate is the majority. Otherwise, the sequence doesn't
contain a majority.

3 External links
Note that the counter can be a maximum of n which requires O(log n) space. In practice, however, a constant
number of bits should suce as a 128 bit counter can go
upto 2128 which is large enough for any practical computation. The time complexity remains O(n) , even considering the amount of time it takes to increment the counter
because it can be incremented in constant amortized time.

4 TEXT AND IMAGE SOURCES, CONTRIBUTORS, AND LICENSES

Text and image sources, contributors, and licenses

4.1

Text

BoyerMoore majority vote algorithm Source: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm?


oldid=733944248 Contributors: Bender235, Ruud Koot, BD2412, Cesium 133, Baiyubin, Yobot, Abigbear, Venkatarun95, ClueBot NG,
Masssly, Everymorning, CAPTAIN RAJU, Oerduo, Petermcottle and Anonymous: 7

4.2

Images

File:Wiki_letter_w.svg Source: https://upload.wikimedia.org/wikipedia/en/6/6c/Wiki_letter_w.svg License: Cc-by-sa-3.0 Contributors:


? Original artist: ?

4.3

Content license

Creative Commons Attribution-Share Alike 3.0

You might also like