给定选举中候选人的姓名数组。数组中的候选人姓名代表对候选人的投票。打印获得最多选票的候选人姓名。如果有平局,请打印按字典顺序排列的较小名称。
例子:
Input : votes[] = {"john", "johnny", "jackie",
"johnny", "john", "jackie",
"jamie", "jamie", "john",
"johnny", "jamie", "johnny",
"john"};
Output : John
We have four Candidates with name as 'John',
'Johnny', 'jamie', 'jackie'. The candidates
John and Johny get maximum votes. Since John
is alphabetically smaller, we print it.
一个简单的解决方案是运行两个循环并计算每个单词的出现次数。此解决方案的时间复杂度为 O(n * n * MAX_WORD_LEN)。
一个有效的解决方案是使用哈希。我们将所有选票插入哈希映射并跟踪不同名称的计数。最后,我们遍历地图并打印出得票最多的人。
C++
// C++++ program to find winner in an election.
#include "bits/stdc++.h"
using namespace std;
/* We have four Candidates with name as 'John',
'Johnny', 'jamie', 'jackie'.
The votes in String array are as per the
votes casted. Print the name of candidates
received Max vote. */
void findWinner(vector& votes)
{
// Insert all votes in a hashmap
map mapObj ;
for (auto& str : votes)
{
mapObj[str]++;
}
// Traverse through map to find the candidate
// with maximum votes.
int maxValueInMap = 0;
string winner;
for (auto& entry : mapObj)
{
string key = entry.first;
int val = entry.second;
if (val > maxValueInMap)
{
maxValueInMap = val;
winner = key;
}
// If there is a tie, pick lexicographically
// smaller.
else if (val == maxValueInMap &&
winner>key)
winner = key;
}
cout << winner << endl;
}
// Driver code
int main()
{
vector votes = { "john", "johnny", "jackie",
"johnny", "john", "jackie",
"jamie", "jamie", "john",
"johnny", "jamie", "johnny",
"john" };
findWinner(votes);
return 0;
}
Java
// Java program to find winner in an election.
import java.util.*;
public class ElectoralVotingBallot
{
/* We have four Candidates with name as 'John',
'Johnny', 'jamie', 'jackie'.
The votes in String array are as per the
votes casted. Print the name of candidates
received Max vote. */
public static void findWinner(String votes[])
{
// Insert all votes in a hashmap
Map map =
new HashMap();
for (String str : votes)
{
if (map.keySet().contains(str))
map.put(str, map.get(str) + 1);
else
map.put(str, 1);
}
// Traverse through map to find the candidate
// with maximum votes.
int maxValueInMap = 0;
String winner = "";
for (Map.Entry entry : map.entrySet())
{
String key = entry.getKey();
Integer val = entry.getValue();
if (val > maxValueInMap)
{
maxValueInMap = val;
winner = key;
}
// If there is a tie, pick lexicographically
// smaller.
else if (val == maxValueInMap &&
winner.compareTo(key) > 0)
winner = key;
}
System.out.println(winner);
}
// Driver code
public static void main(String[] args)
{
String[] votes = { "john", "johnny", "jackie",
"johnny", "john", "jackie",
"jamie", "jamie", "john",
"johnny", "jamie", "johnny",
"john" };
findWinner(votes);
}
}
Python3
# Python3 program to find winner in an election.
from collections import defaultdict
''' We have four Candidates with name as 'John',
'Johnny', 'jamie', 'jackie'.
The votes in String array are as per the
votes casted. Print the name of candidates
received Max vote. '''
def findWinner(votes):
# Insert all votes in a hashmap
mapObj = defaultdict(int)
for st in votes:
mapObj[st] += 1
# Traverse through map to find the
# candidate with maximum votes.
maxValueInMap = 0
winner = ""
for entry in mapObj:
key = entry
val = mapObj[entry]
if (val > maxValueInMap):
maxValueInMap = val
winner = key
# If there is a tie, pick lexicographically
# smaller.
elif (val == maxValueInMap and
winner > key):
winner = key
print(winner)
# Driver code
if __name__ == "__main__":
votes = [ "john", "johnny", "jackie",
"johnny", "john", "jackie",
"jamie", "jamie", "john",
"johnny", "jamie", "johnny",
"john" ]
findWinner(votes)
# This code is contributed by ukasp
C#
// C# program to find winner in an election.
using System;
using System.Collections.Generic;
public class ElectoralVotingBallot
{
/* We have four Candidates with name as 'John',
'Johnny', 'jamie', 'jackie'.
The votes in String array are as per the
votes casted. Print the name of candidates
received Max vote. */
public static void findWinner(String []votes)
{
// Insert all votes in a hashmap
Dictionary map =
new Dictionary();
foreach (String str in votes)
{
if (map.ContainsKey(str))
map[str] = map[str] + 1;
else
map.Add(str, 1);
}
// Traverse through map to find the candidate
// with maximum votes.
int maxValueInMap = 0;
String winner = "";
foreach(KeyValuePair entry in map)
{
String key = entry.Key;
int val = entry.Value;
if (val > maxValueInMap)
{
maxValueInMap = val;
winner = key;
}
// If there is a tie, pick lexicographically
// smaller.
else if (val == maxValueInMap &&
winner.CompareTo(key) > 0)
winner = key;
}
Console.WriteLine(winner);
}
// Driver code
public static void Main(String[] args)
{
String[] votes = { "john", "johnny", "jackie",
"johnny", "john", "jackie",
"jamie", "jamie", "john",
"johnny", "jamie", "johnny",
"john" };
findWinner(votes);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
John
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。