This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like this: Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. We have to solve in O(n) time and O(1) Space. URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
|
>>> majority_vote([1, 2, 2, 3, 1, 3, 2], 3)
[2]
>>> majority_vote([1, 2, 2, 3, 1, 3, 2], 2)
[]
>>> majority_vote([1, 2, 2, 3, 1, 3, 2], 4)
[1, 2, 3]