Find the candidate with the "popular vote." This is the candidate having the most number of first-choice votes from the electorate.
Find the "ranked choice" winner. This is calculated as follows:
- If there is a candidate who has more than half of the first-choice votes, then that candidate is the clear winner.
- If there are multiple candidates with the same number of first-choice votes, remove the candidate with the least number of first-choice votes. Recalculate the choice votes and return the candidate with the most first-place votes. Continue this process until either a candidate with a clear majority (> 50% of votes) is found or only a single candidate remains.
Example: {([A, B, C] -> 4), ([B, C, A] -> 3), ([C, B, A] -> 2)}
This means that candidate A received 4 votes as the electorate's first choice, candidate B received 4 votes as the electorate's second choice, and candidate C received 4 votes as the electorate's third choice. Continuing further, candidate B received 3 votes as the electorate's first choice, candidate C received 3 votes as the electorate's second choice, and candidate A received 3 votes as the electorate's third choice, and so on.
Method 1: A (Max ballot rank 1 points)
Method 2: B (Remove the candidate with the least ballot points and compare the max ballot rank 1 points. If the max rank points are greater than 50% of other ballot winners, choose them as the winner).
Solution:
Popular winner: Candidate A is the popular winner, having received the most number of votes as the electorate's first choice.
Ranked winner: In this case, although candidate A received the most first-choice votes, it is not a clear majority (4 is not 50% of the total of 9 ballots). So, we remove the votes for the candidate with the least number of first-choice votes, which is C. Now we are left with {([A, B] -> 4), ([B, A] -> 3), ([B, A] -> 2)}, giving us the winner as candidate B, who now has 5 votes as the electorate's first choice.
java
private String findPopularCandidate(Map<String, Integer> ballots) {
// your code here
}
private String findRankedWinner(Map<String, Integer> ballots) {
// your code here
}