blob: 8c67b749551a02c5764af6933db0f41ea6d5c166 (
plain) (
tree)
|
|
#+title: boyer-moore
#+date: <2018-06-04>
The
[[https://en.wikipedia.org/wiki/Boyer–Moore_majority_vote_algorithm][Boyer-Moore
algorithm for finding the majority of a sequence of elements]] falls in
the category of "very clever algorithms".
#+begin_src c++
int majorityElement(vector<int>& xs) {
int count = 0;
int maj = xs[0];
for (auto x : xs) {
if (x == maj) count++;
else if (count == 0) maj = x;
else count--;
}
return maj;
}
#+end_src
|