Find winner of an election where votes are represented as candidate names | Python3 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 . ; Insert all votes in a hashmap ; Traverse through map to find the candidate with maximum votes . ; If there is a tie , pick lexicographically smaller . ; Driver code
from collections import defaultdict NEW_LINE def findWinner ( votes ) : NEW_LINE INDENT mapObj = defaultdict ( int ) NEW_LINE for st in votes : NEW_LINE INDENT mapObj [ st ] += 1 NEW_LINE DEDENT maxValueInMap = 0 NEW_LINE winner = " " NEW_LINE for entry in mapObj : NEW_LINE INDENT key = entry NEW_LINE val = mapObj [ entry ] NEW_LINE if ( val > maxValueInMap ) : NEW_LINE INDENT maxValueInMap = val NEW_LINE winner = key NEW_LINE DEDENT elif ( val == maxValueInMap and winner > key ) : NEW_LINE INDENT winner = key NEW_LINE DEDENT DEDENT print ( winner ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT votes = [ " john " , " johnny " , " jackie " , " johnny " , " john " , " jackie " , " jamie " , " jamie " , " john " , " johnny " , " jamie " , " johnny " , " john " ] NEW_LINE findWinner ( votes ) NEW_LINE DEDENT