Find the Winner
Posted: 17 Jan, 2021
You have been given an array/list of “VOTES” which contains the name of the candidates where each entry represents the name of the candidate who got the vote.
You are supposed to find the name of the candidate who received the maximum number of votes. If there is a tie, then print the lexicographically smaller name.
Input Format :
The first line contains an integer ‘T’ denoting the number of test cases. Then each test case follows. The first input line of each test case contains an integer ‘N’ denoting the total number of votes cast. Each of the next ‘N’ lines contains the name of the candidate who received the vote.
Output Format :
For each test case, print the name of the candidate who received the maximum number of votes. Print the output of each test case in a separate line.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 50 1 <= ‘N’ <= 10^3 1 <= |NAME| <= 20 Where ‘N’ is the number of votes cast and |NAME| denotes the length of the candidate’s name. Time Limit: 1 sec
The basic idea of this approach is to iterate through each candidate and count the number of votes he/she has received. We will keep track of the candidate with maximum votes.
Here is the algorithm :
- Create a variable “WINNER” which stores the name of the winning candidate and also create a variable “MAXVOTE” which stores the count of the vote the winning candidate has received.
- Iterate through the given array/list of votes using a variable ‘I’.
- Create a nested loop to count the number of votes the candidate has received.
- Update the “WINNER” and “MAXVOTE” if the current candidate has received more votes than the “MAXVOTE”. Also, in case “MAXVOTE” is equal to the votes received by the current candidate, update the “WINNER” as the lexicographically smaller name.
One Odd Occurring
Posted: 17 Apr, 2022
Posted: 5 May, 2022
Left Rotate an Array by One
Posted: 17 May, 2022
Largest Element in the Array
Posted: 17 May, 2022
Matrix Boundary Traversal
Posted: 20 May, 2022