Algorithms · Visualized

Majority Element

Divide & Conquer

Break a problem into independent subproblems, solve each, and combine the answers.

Finds the element that appears more than n/2 times in an array. Time: O(n log n) average, O(n log n) — note the Boyer-Moore Voting Algorithm solves this in O(n), D&C is a teaching alternative worst; space: O(log n) — recursion stack.

Big-O: Classic D&C recursions: Merge Sort, Quick Sort, Maximum Subarray, Fast Power.

Majority Element (Divide & Conquer)

Divide & Conquer

Finds the element that appears more than n/2 times in an array. Using divide and conquer: split the array in half, find the majority candidate of each half recursively, and if they differ, count actual occurrences of each candidate in the combined range to decide the true majority.

How Majority Element Works

  1. Base case: if the array has one element, it is trivially the majority element of that single-element range.
  2. Split the array into left and right halves.
  3. Recursively find the majority candidate of the left half and of the right half.
  4. If both halves agree on the same candidate, that's the majority element for the combined range.
  5. If they disagree, count how many times each candidate actually appears across the full combined range, and return whichever count is higher.

Majority Element Worked Example

Input: [2, 2, 1, 1, 1, 2, 2]

Split into [2,2,1,1] and [1,2,2]. Left half recursively resolves to majority candidate 2 (or a tie broken by counting). Right half resolves to 2. Both agree on 2 → combined majority candidate is 2. Verifying: 2 appears 4 times out of 7, more than half — confirmed majority element is 2.

Majority ElementO(n)
Boyer–Moore voting pairs up distinct values that cancel each other.
2
0
2
1
1
2
1
3
1
4
2
5
2
6
Step 1 / 9
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Majority Element (Boyer–Moore voting): cancel different elements against each other.
Python · Divide & Conquer in Code
def majority_element(a):
    cand, count = None, 0
    for v in a:
        if count == 0:
            cand, count = v, 1
        elif v == cand:
            count += 1
        else:
            count -= 1
    return cand

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Majority Element Time & Space Complexity

BestO(n log n)
AverageO(n log n)
WorstO(n log n) — note the Boyer-Moore Voting Algorithm solves this in O(n), D&C is a teaching alternative
SpaceO(log n) — recursion stack

When to Use Majority Element

  • Teaching how divide-and-conquer 'combine' steps sometimes require extra verification work, unlike simpler merge steps.
  • Voting/consensus systems needing to identify a dominant choice among many votes.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.