Skip to the code
cf.
+10 XP

#67 Poll Winner With a Majority

easy

A settings page asked users to vote for their favorite theme. The votes are in votes, and one theme is guaranteed to have more than half of them.

Write pollWinner(votes) that returns that theme. Try to use only O(1) extra memory.

Example 1

Input: votes = ["dark","light","dark","dark","system"]
Output: "dark"

dark has 3 of the 5 votes.

Example 2

Input: votes = ["a"]
Output: "a"

A single vote is a majority.

Constraints

  • 1 <= votes.length <= 10^5
  • One value appears more than votes.length / 2 times.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).