Ctrl K

The Candidate With Most Votes

Easy
An election has just finished: each element of votes is the number of the candidate a ballot went to. One candidate is known to have received **strictly more than half** of all the votes. Return that candidate's number. Counting with a dictionary is the obvious route. Can you do it in linear time with O(1) extra memory?
Example 1
votes=[7, 4, 7, 7, 2] → 7

Candidate 7 got 3 of the 5 votes, which is more than half.

Example 2
votes=[-5] → -5

A single ballot is a majority on its own.

Example 3
votes=[1, 9, 9, 1, 9, 1, 9] → 9

9 has four of the seven votes, while 1 has only three.

Constraints
  • 1 ≤ len(votes) ≤ 5·10⁴
  • -10⁹ ≤ votes[i] ≤ 10⁹
  • A candidate with more than len(votes) / 2 votes always exists
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Common Values MediumValues One Apart EasyPresent in Every List EasyMore Than a Third of the Votes MediumJobs With a Cooldown Medium
loading Python…
Input
votes=[7, 4, 7, 7, 2]
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 41 tests on the server, hidden ones included.