Find winner of an election where votes are represented as candidate names | 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
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 ) ; } 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 ; } else if ( val == maxValueInMap && winner > Key ) winner = Key ; } document . write ( winner ) ; } let votes = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; findWinner ( votes ) ;