Gregable: Majority Voting Algorithm - Find the majority element in a list of values
https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
candidate = 0
count = 0
for candidate_i, count_i in parallel_output:
if candidate_i = candidate
count += count_i
else if count_i > count:
count = count_i - count
candidate = candidate_i
else
count = count - count_i
Read full article from Gregable: Majority Voting Algorithm - Find the majority element in a list of values
https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
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 { 11 if (i == candidate) { 12 counter++; 13 } else { 14 counter--; 15 } 16 } 17 } 18 19 counter = 0; 20 for (int i : num) { 21 if (i == candidate) counter++; 22 } 23 if (counter < (n + 1) / 2) return -1; 24 return candidate; 25 26 }
Distributed Boyer-Moore
candidate = 0
count = 0
for candidate_i, count_i in parallel_output:
if candidate_i = candidate
count += count_i
else if count_i > count:
count = count_i - count
candidate = candidate_i
else
count = count - count_i
Read full article from Gregable: Majority Voting Algorithm - Find the majority element in a list of values