forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
30.12.2023.cpp
33 lines (28 loc) · 865 Bytes
/
30.12.2023.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution{
public:
//Function to return the name of candidate that received maximum votes.
vector<string> winner(string arr[],int n)
{
// Your code here
// Return the string containing the name and an integer
// representing the number of votes the winning candidate got
unordered_map<string, int>mp;
int count=0;
for(int i=0;i<n;i++){
mp[arr[i]]++;
if(count < mp[arr[i]]) count=mp[arr[i]];
}
string ans="";
for(int i=0;i<n;i++){
if(mp[arr[i]]==count){
if(ans==""){
ans=arr[i];
}
else if(arr[i] < ans){
ans= arr[i];
}
}
}
return {ans, to_string(count)};
}
};