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.
- Implement the Boyer-Moore Voting Algorithm:
- Initialize a
candidateand acountof 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.
- If the count is 0, set the current number as the
- Initialize a
- Print the final
candidate.
01EXAMPLE 1
Input
[3, 2, 3]Output
3Explanation: 3 appears 2/3 times.
Constraints
- Solve in O(n) time.
- Solve in O(1) extra space (no Map/Object).
ArraysAlgorithms
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
votes[3,2,3]
Expected Output
3
Click RUN to test your solution