Winner of an election | Practice | GeeksforGeeks

Given an array of names of candidates in an election. A candidate name in array represents a vote casted to the candidate. The task is to print the name of candidates received maximum vote. If there is tie, print lexicographically smaller name.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. First line of each test case contains an Integer N denoting number of votes and the second line contains N space separated strings.  
Output:
For each test case, print the winner of election in new line.
Constraints:
1<=T<=100
2<=N<=105
1<=|String length|<=20
Example:
Input:
2
6
aaa bbb ccc bbb aaa  aaa
7
geeks for geeks for geeks aaa aaa
Output:
aaa
geeks
0
rathoredivya1502 months ago
#include <bits/stdc++.h>using namespace std;
int main() {   int t;   cin>>t;   while(t--)   {string st;       map<string,int>mp;       int n;       cin>>n;       string s[n];       for(int i=0;i<n;i++)       {string str;       cin>>str;           mp[str]++;       }int mx=INT_MIN;       for(auto a:mp)       {           if(mx<a.second)           {               mx=a.second;               st=a.first;           }       }       cout<<st<<"\n";   }//codereturn 0;}
0
snipperwolf3 months ago
for _ in range(int(input())):   n=int(input())   n1=input()   mp={}   for i in n1.split(' '):       if i not in mp:           mp[i]=1       else:           mp[i]+=1   n=max(mp.values())   l=[]   for i,v in mp.items():       if v==n:           l.append(i)   if len(l)==1:       print(l[0])   else:       print(min(l))
0
kashyapjhon3 months ago
C++ Solution:
#include <bits/stdc++.h>using namespace std;int main() {//codeint t;cin>>t;// cin.ignore();while(t--){    map<string,int> s;    int n;    cin>>n;    for(int i=0;i<n;i++){        string str;        cin>>str;        s[str]++;    }    int t=INT_MIN;    for(auto it=s.begin();it!=s.end();it++){        if(t<it->second){            t=it->second;        }    }    auto ans=s.begin();    for(auto it=s.begin();it!=s.end();it++){        if(it->second==t){            ans=it;            break;        }    }    cout<<ans->first<<endl;}return 0;}
+1
badgujarsachin835 months ago
#include<iostream>
#include<map>
using namespace std;
int main()
 {
	//code
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    string arr[100000];
	    for(int i=0;i<n;i++){
	        cin>>arr[i];
	    }
	    map<string,int> mp;
	    int maxi=0;
	    string name;
        for(int i=0;i<n;i++){
        mp[arr[i]]++;
        maxi=max(maxi,mp[arr[i]]);
       }
	   for(auto it:mp){
	        if(it.second==maxi ){
	           if(name.empty()||it.first<name){
	               name=it.first;
	           }
	        }
	    }
	    cout<<name<<endl;
	}
	return 0;
}
0
msaravanansara4746 months ago
for _ in range(int(input())):
    input()
    v=input().split()
    dictorial={}
    for x in v:
        if x in dictorial:
            dictorial[x]+=1
        else:
            dictorial[x]=1
    b=max(dictorial.values())
    for x in sorted(dictorial.keys()):
        if dictorial[x]==b:
            print(x)
            break
-1
aradhyakanthofficial6 months ago
Solution in Python:
#function define
def task(l):   l2=list(set(l))   dict1=dict()   for i in l2:       dict1[i]=l.count(i)   c=dict1.items()   d=sorted(c,key=lambda x:x[1],reverse=True)   e=[d[i][0] for i in range(len(d)) if d[i][1]==d[0][1]]   e.sort()   print(e[0])
 
#Driver code          
t=int(input())while(t>0):   n=int(input())   l=input().split()   l=l[:n]   task(l)   t-=1
-2
imranwahid7 months ago
Easy C++ solution
https://ide.geeksforgeeks.org/exe78nbVCO
0
Mohit Kesharwani7 months ago
Mohit Kesharwani
#include<iostream>#include <bits stdc++.h="">using namespace std;int main() {//code//mohit.ke000int t;cin>>t;while(t--){    int n;    cin>>n;    string s[n];    for(int i=0; i<n; i++)="" {="" cin="">>s[i];    }
    map<string,int>mp;        for(int i=0;i<n; i++)="" {="" mp.insert({s[i],i});="" mp[s[i]]++;="" }="" map<string,int="">::iterator it;            int max=0;        for(it=mp.begin(); it!=mp.end(); it++)        {           // cout<<it->first<<" "<<it->second<<endl; if(max="" <="" it-="">second)             {                 max=it->second;             }        }        for(it=mp.begin(); it!=mp.end(); it++)         {            if(max==it->second)            {                cout<<it->first;                break;            }         }
         cout<<endl; }="" return="" 0;="" }="">
0
MD DANISH_IT_2nd_A_249 months ago
MD DANISH_IT_2nd_A_24
#include <bits stdc++.h="">using namespace std;
int main() {    int t;    cin>>t;    while(t--){        int n;        cin>>n;        string s;        map<string, int="">m;        for(int i=0;i<n;i++){ cin="">>s;            m[s]++;            s.clear();        }        int p=INT_MIN;        string ans;        for(auto it:m){            if(it.second > p){                p = it.second;                ans = it.first;            }        }        cout<<ans<<endl; }="">
0
Laxman Kumar vashist9 months ago
Laxman Kumar vashist
JAVA Solution ... https://uploads.disquscdn.c...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.