Majority Vote

HardAcc. 75.3%
+45 XP 25

The Consensus Peak (Majority Element)

In a democracy of data, the "Majority Element" is the one that appears more than half the time. Your goal is to find this dominant value. Because it is so frequent, it will always "win" in a head-to-head vote against all other elements combined.

The Assignment

Your mission is to find the majority winner. Your function receives an array nums.

  1. Implement the Boyer-Moore Voting Algorithm:
    • Initialize a candidate and a count of 0.
    • For each number:
      • If the count is 0, set the current number as the candidate.
      • If the number matches the candidate, increase the count.
      • Otherwise, decrease the count.
  2. Print the final candidate.

01EXAMPLE 1

Input[3, 2, 3]
Output3

Explanation: 3 appears 2/3 times.

Constraints

  • Solve in O(n) time.
  • Solve in O(1) extra space (no Map/Object).
ArraysAlgorithms
JavaScript
Loading...
2 Hidden

Input Arguments

votes[3,2,3]

Expected Output

3

Click RUN to test your solution