Field Trip Vote
Your class is voting on a field trip. Each student writes one destination code on a slip: 4 for the aquarium, 9 for the science museum, 17 for the climbing wall, and so on. A trip goes ahead only if one destination gets more than half the votes, and the teacher knows that happened. You need to find the winning destination.
Your task
Write winningTrip(votes). Return the destination code that appears more than votes.length / 2 times.
Such a destination always exists, so there is always exactly one correct answer.
winningTrip(votes: int[]) → int
Examples
votes = [9, 4, 9, 17, 9]
9
There are five votes, so the winner needs at least 3. The science museum, code 9, got 3.
votes = [4, 4, 17, 17, 17, 17, 4]
17
There are seven votes, so the winner needs at least 4. Code 17 got exactly 4, and it does not matter that the first vote was a 4.
Constraints
- 1 ≤ votes.length ≤ 50,000
- 1 ≤ votes[i] ≤ 1,000,000
- One value appears more than votes.length / 2 times.
Start with brute force
For each vote, scan the whole box, count the matching slips, and return the first code that is past half. That takes O(n²) time, which is up to 2.5 billion comparisons for 50,000 votes. Sorting also works, since the winner must fill the middle position, but it costs O(n log n) and reorders the slips.
Hints
0 of 4 shown- Hint 1
With 7 votes, work out how many votes the winner needs. Once a destination reaches that number, no other destination can beat it.
- Hint 2
You need each destination's vote count so far, like a tally sheet with one line per destination.
- Hint 3
Use a structure that keeps a count per code even when you do not know in advance which codes will appear.
- Hint 4
Walk the votes once with a map from code to count. After each vote, if that count is now more than
votes.length / 2, return that code.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The solution reads the slips once and keeps a running tally for each destination. As soon as one destination passes half the class, it has won.
The trace below uses [9, 4, 9, 17, 9]. Half of 5 is 2.5, so the winner needs 3.
- The vote 9 raises the tally for 9 to 1.
- The vote 4 raises the tally for 4 to 1.
- The second vote 9 raises its tally to 2, which is not yet more than 2.5.
- The vote 17 raises the tally for 17 to 1.
- The third vote 9 raises its tally to 3, which is more than 2.5, so the function returns 9.
Compare each tally against the total number of votes, not the number of votes read so far. After the first slip, its destination has 100% of the votes read but has not won.
A solution with O(1) memory also exists, which keeps one candidate and a counter. Add 1 when a vote matches the candidate, subtract 1 when it does not, and switch candidate when the counter hits 0. Each losing vote cancels one winning vote, and the winner outnumbers everyone else combined, so it is left standing.
function winningTrip(votes) {
const count = new Map(); // destination -> votes so far
for (const v of votes) {
const c = (count.get(v) || 0) + 1;
count.set(v, c);
if (c > votes.length / 2) return v; // nobody can catch up now
}
return -1; // never reached: a winner always exists
}def winning_trip(votes):
count = {} # destination -> votes so far
for v in votes:
count[v] = count.get(v, 0) + 1
if count[v] > len(votes) / 2: # nobody can catch up now
return v
return -1 # never reached: a winner always existsRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.