{"id":270,"date":"2026-11-13T12:36:30","date_gmt":"2026-11-13T04:36:30","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-boyer-moore-vote-finds-a-majority-candidate\/"},"modified":"2026-11-13T21:54:07","modified_gmt":"2026-11-13T13:54:07","slug":"how-the-boyer-moore-vote-finds-a-majority-candidate","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-boyer-moore-vote-finds-a-majority-candidate\/","title":{"rendered":"How the Boyer-Moore vote finds a majority candidate"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Boyer-Moore majority vote<\/strong> finds a candidate that is the only value that could be a strict majority of an array, using one counter and one candidate. It scans once. On a match it increments. On a mismatch it decrements. At zero it takes the current value as the new candidate. A second pass counts the candidate and checks that it appears more than n\/2 times. The first pass alone does not prove a majority. Together the passes are <strong>O(n)<\/strong> time and <strong>O(1)<\/strong> extra memory.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">If a value occupies more than half the positions, it cannot be cancelled by pairing each of its occurrences against a different value. The counter is that pairing. Whenever the counter returns to zero, a whole prefix has been paired away, and any majority of the whole array is still a majority of what remains. The surviving candidate is therefore the only possible majority element.<\/p>\n\n\n<p class=\"wp-block-paragraph\">If there is no majority, the same pairing still produces some candidate. The first pass has no way to know. The second pass counts. Skipping it reports a winner in an array that has none.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">If a strict majority element exists, the first pass returns it. If none exists, the first pass returns an arbitrary survivor. The second pass distinguishes the cases by an exact count. Time is \u0398(n) and extra memory is two integers. The algorithm finds an element that occurs more than n\/2 times, not the mode of a multiset that is merely the most frequent. A value at exactly n\/2 is not a strict majority and must fail the second pass.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A generalization keeps k\u22121 candidates to find every value that occurs more than n\/k times. That is the Misra-Gries summary. The k = 2 case is this vote.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Set count to 0 and leave the candidate unset.<\/li>\n<li>For each value, if count is 0, set the candidate to the value and the count to 1. Else if the value equals the candidate, increment. Else decrement.<\/li>\n<li>After the scan, count the occurrences of the candidate in a second pass.<\/li>\n<li>If the count is greater than n\/2, return the candidate. Otherwise report that no majority exists.<\/li>\n<li>Do not skip the second pass because the problem statement promised a majority. A later caller will reuse the function on data that has none.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The O(1) memory bound is for one majority candidate. The mode of an unrestricted multiset cannot be found in one pass with constant memory in the comparison model. Misra-Gries is the heavy-hitters generalization. The vote is not a sort and not a median. The median can be found in linear time by quickselect and is a different element.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the first pass is trusted without a count<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Return the candidate immediately<\/td>\n<td>An array with no majority still produces a candidate. The second pass is the proof.<\/td>\n<\/tr>\n<tr>\n<td>Accept a count of exactly n\/2<\/td>\n<td>A strict majority is more than half. A tie between two values is not a majority for either.<\/td>\n<\/tr>\n<tr>\n<td>Decrement on a match<\/td>\n<td>The pairing then cancels the value against itself. The candidate of a real majority can be lost.<\/td>\n<\/tr>\n<tr>\n<td>Use the algorithm to find the most common value below n\/2<\/td>\n<td>No such value is guaranteed to survive the pairing. The theorem is only about a strict majority.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Heavy hitters above 1\/k are Misra-Gries. A median is quickselect. The Boyer-Moore vote is the constant-memory majority candidate plus a verifying count.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Bash script and the Python script run the Boyer\u2013Moore majority vote, then count the candidate. Both print <code>2<\/code>, the only value that occurs more than n\/2 times.<\/p>\n\n<pre><code class=\"language-bash\">a=(2 2 1 2 3 2 2)\ncount=0\ncand=0\nfor v in &quot;${a[@]}&quot;; do\n  if (( count == 0 )); then cand=$v; count=1\n  elif (( v == cand )); then count=$((count+1))\n  else count=$((count-1)); fi\ndone\nseen=0\nfor v in &quot;${a[@]}&quot;; do\n  if (( v == cand )); then seen=$((seen+1)); fi\ndone\nif (( seen * 2 &gt; ${#a[@]} )); then echo &quot;$cand&quot;; else echo none; fi\n<\/code><\/pre>\n<pre><code class=\"language-python\">a = [2, 2, 1, 2, 3, 2, 2]\ncount = 0\ncand = None\nfor v in a:\n    if count == 0:\n        cand, count = v, 1\n    elif v == cand:\n        count += 1\n    else:\n        count -= 1\nassert a.count(cand) &gt; len(a) \/ 2\nprint(cand)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Boyer-Moore vote finds a majority element in one pass and constant memory: a value that occupies more than half of a stream, if one exists. A second pass confirms it. Misra-Gries is the generalization that tracks several heavy hitters.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the verifying pass only when a majority is an invariant of the input and a wrong answer is acceptable if that invariant was already broken. Otherwise count.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the Boyer-Moore majority vote returns the pairing counter and the second pass that proves the majority. <strong>The Boyer-Moore vote finds the only possible strict majority in one counter pass, and a second counting pass is what confirms the candidate really exceeds n\/2.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: The Boyer-Moore majority vote finds a candidate that is the only value that could be a strict majority of an array, using one counter and one candidate. It scans once. On a match it increments. On a mismatch it decrements. At zero it takes the current value&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,44],"tags":[],"class_list":["post-270","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-sequences"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=270"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/270\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/270\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}