intermediate · voting algorithm · arrays · counting

Which candidate has more than half the votes?

Problem

A ballot box contains n votes, each a candidate number. One candidate is guaranteed to have more than n/2 votes. Find that candidate using O(1) extra space.

Constraints

  • 1 ≤ n ≤ 32
  • votes from −999 to 999
  • a strict majority exists (validated)

Examples

[2, 2, 1, 1, 1, 2, 2] → 2

Show the solution

Boyer–Moore majority vote

Each −1 cancels one vote for the candidate against one vote for someone else. The majority has more votes than everyone else combined, so it cannot be fully cancelled.

Why it is correct

Whenever the counter returns to 0, the prefix seen so far can be discarded: the majority still holds a strict majority in the rest. At the end the surviving candidate is the majority.

Complexity

Time O(n), extra space O(1). One pass with O(1) work per vote.

For contrast: Count every candidate with a hash map, or sort and take the middle. That costs O(n) expected / O(n log n) time and O(n) / O(1) space.

Edge cases

  • One vote.
  • Without the guarantee, a second pass must verify the candidate.

Common mistakes

  • Returning the candidate without verification when no majority is guaranteed.
  • Adopting a new candidate before checking the counter.

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class MajorityElement
{
    /// <summary>The value with more than n/2 votes (Boyer-Moore). Precondition: one exists. O(n), O(1) space.</summary>
    public static int Find(int[] votes)
    {
        int candidate = 0, count = 0;
        foreach (int vote in votes)
        {
            if (count == 0)
                candidate = vote;
            count += vote == candidate ? 1 : -1;
        }
        return candidate;
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/MajorityElement.cs

from collections.abc import Iterable
 
 
def find(votes: Iterable[int]) -> int:
    """The value with more than n/2 votes (Boyer-Moore). Precondition: one exists. O(n), O(1) space."""
    candidate, count = 0, 0
    for vote in votes:
        if count == 0:
            candidate = vote
        count += 1 if vote == candidate else -1
    return candidate

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/majority_element.py

package dev.bekadan.dsa.problems;
 
public final class MajorityElement {
    private MajorityElement() {
    }
 
    /** The value with more than n/2 votes (Boyer-Moore). Precondition: one exists. O(n), O(1) space. */
    public static int find(int[] votes) {
        int candidate = 0, count = 0;
        for (int vote : votes) {
            if (count == 0) {
                candidate = vote;
            }
            count += vote == candidate ? 1 : -1;
        }
        return candidate;
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/MajorityElement.java

#pragma once
 
#include <span>
 
namespace bekadan::dsa::problems {
 
// The value with more than n/2 votes (Boyer-Moore). Precondition: one exists. O(n), O(1) space.
inline int majority_element(std::span<const int> votes) {
    int candidate = 0, count = 0;
    for (int vote : votes) {
        if (count == 0)
            candidate = vote;
        count += vote == candidate ? 1 : -1;
    }
    return candidate;
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/majority_element.hpp