在选举中给出一系列候选人的名字。数组中的候选人名称代表投票给该候选人。打印获得最多选票的候选人的姓名。如果有领带,打印一个按字典顺序较小的名字。
null
例如:
Input : votes[] = {"john", "johnny", "jackie", "johnny", "john", "jackie", "jamie", "jamie", "john", "johnny", "jamie", "johnny", "john"};Output : JohnWe have four Candidates with name as 'John', 'Johnny', 'jamie', 'jackie'. The candidatesJohn and Johny get maximum votes. Since Johnis alphabetically smaller, we print it.
A. 简单解决方案 就是运行两个循环并计算每个单词的出现次数。这个解决方案的时间复杂度是O(n*n*MAX\u WORD\u 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<string>& votes) { // Insert all votes in a hashmap unordered_map<string, int > 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<string> 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<String,Integer> map = new HashMap<String, Integer>(); 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<String,Integer> 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<String, int > map = new Dictionary<String, int >(); 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<String, int > 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
<script> // Javascript program to find winner in an election. /* 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. */ function findWinner(votes) { let map = new Map(); for (let i=0;i<votes.length;i++) { if (map.has(votes[i])) { map.set(votes[i], map.get(votes[i]) + 1); } else map.set(votes[i], 1); } // Traverse through map to find the candidate // with maximum votes. let maxValueInMap = 0; let winner = "" ; for (let [key, value] of map.entries()) { let Key = key; let val = value; if (val > maxValueInMap) { maxValueInMap = val; winner = key; } // If there is a tie, pick lexicographically // smaller. else if (val == maxValueInMap && winner>Key) winner = Key; } document.write(winner); } // Driver code let votes=[ "john" , "johnny" , "jackie" , "johnny" , "john" , "jackie" , "jamie" , "jamie" , "john" , "johnny" , "jamie" , "johnny" , "john" ]; findWinner(votes); // This code is contributed by rag2127 </script> |
输出:
John
另一个有效的解决方案是使用 崔 .请参考 字符串数组中最常用的单词 . 本文由 伊什法克·拉姆赞·纳古 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END