top three candidates in an election

  • 1

    Write a program to find top two candidates winning the election.

    You are the administrator of an election system. The back end is offline and election results are requested.
    All votes are logged to vote.log(stream). We want to see results in real time.


    • A single voter can vote for 3 candidates.
    • A voter cannot vote for the same candidate more than once.

    Using the log find the top 5 candidates and detect fraud by printing the voter id and not counting the vote.

    Fraud is candidate voting more than 3 times and/or a candidate more than once.

  • 0

    @GG Thanks for the interesting question. Here are my thoughts:

    1. Use a Map<Voter, Integer> to track how many candidates a voter voted. If < 3, accept this vote, otherwise, deny and print out this voter as fraud.
    2. Use a Map<Candidate, Integer> to track how many tickets each candidate got.
    3. Use a Heap<Map.Entry<Candidate, Integer>> to monitor the top 2 candidates.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.