Dictionary and counter in Python to find winner of election
Given an array of names of candidates in an election. A candidate name in array represents a vote casted to the candidate. Print the name of candidates received Max vote. If there is tie, print lexicographically smaller name.
Examples:
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.
We have existing solution for this problem please refer Find winner of an election where votes are represented as candidate names link. We can solve this problem quickly in python using Dictionary data structure. Approach is very simple,
- Convert given list of votes into dictionary using Counter(iterator) method. We will have a dictionary having candidate names as Key and their frequency ( counts ) as Value.
- Since more than 1 candidate may get same number of votes and in this situation we need to print lexicographicaly smaller name, so now we will create another dictionary by traversing previously created dictionary, counts of votes will be Key and candidate names will be Value.
- Now find value of maximum vote casted for a candidate and get list of candidates mapped on that count value.
- Sort list of candidates having same number of maximum votes and print first element of sorted list in order to print lexicographicaly smaller name.
# Function to find winner of an election where votes # are represented as candidate names from collections import Counter def winner(input): # convert list of candidates into dictionary # output will be likes candidates = {'A':2, 'B':4} votes = Counter(input) # create another dictionary and it's key will # be count of votes values will be name of # candidates dict = {} for value in votes.values(): # initialize empty list to each key to # insert candidate names having same # number of votes dict[value] = [] for (key,value) in votes.iteritems(): dict[value].append(key) # sort keys in descending order to get maximum # value of votes maxVote = sorted(dict.keys(),reverse=True)[0] # check if more than 1 candidates have same # number of votes. If yes, then sort the list # first and print first element if len(dict[maxVote])>1: print sorted(dict[maxVote])[0] else: print dict[maxVote][0] # Driver program if __name__ == "__main__": input =['john','johnny','jackie','johnny','john','jackie','jamie','jamie', 'john','johnny','jamie','johnny','john'] winner(input) |
Output:
john
Recommended Posts:
- Find winner of an election where votes are represented as candidate names
- Python dictionary, set and counter to check if frequencies can become same
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python Counter| Find all duplicate characters in string
- Python Counter| Find duplicate rows in a binary matrix
- Python Counter to find the size of largest subset of anagram words
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Python | Find depth of a dictionary
- Python | Find the closest Key in dictionary
- Python program to find the sum of all items in a dictionary
- Python | Find dictionary matching value in list
- Python program to find second maximum value in Dictionary
- Find the first repeated word in a string in Python using Dictionary
- Python Dictionary to find mirror characters in a string
- Python program to find the highest 3 values in a dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



