Majority Element with Boyer–Moore majority vote algorithm
This algorithm finds a majority element, if there is one: that is, an element that occurs repeatedly for more than half of the elements of the input in linear time O(n) and a constant space O(1) .
A majority element is not equal to the most frequent element.
If you don’t know whether there is a majority element and you want to return the majority element if there exists; Otherwise return None. Make a second pass through the data to verify that the element is a majority.
References: