Wednesday, November 1, 2017

Boyer–Moore majority vote algorithm

169. Majority Element
Wiki: Boyer–Moore majority vote algorithm
http://www.geeksforgeeks.org/majority-element/
findCandidate(a[], size)
1.  Initialize index and count of majority element
     maj_index = 0, count = 1
2.  Loop for i = 1 to size – 1
    (a) If a[maj_index] == a[i]
          count++
    (b) Else
        count--;
    (c) If count == 0
          maj_index = i;
          count = 1
3.  Return a[maj_index]

No comments:

Post a Comment