Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Winner of an election

Easy Acceptance 53.75% Points 20.00

Given an array of n names arr of candidates in an election, where each name is a string of lowercase characters. A candidate name in the array represents a vote casted to the candidate. Print the name of the candidate that received the maximum count of votes. If there is a draw between two candidates, then print lexicographically smaller name.

Examples
Example 1

Example 1

Input: n = 5, arr[] = {alice, bob, alice, charlie, bob}

Output: alice 2

Explanation: Alice and Bob both received 2 votes each. Since Alice is lexicographically smaller than Bob, we print Alice and the number of votes she received.

Example 2

Example 2

Input: n = 7, arr[] = {zara, adam, zara, mike, adam, mike, adam}

Output: adam 3

Explanation: Adam received 3 votes, while Zara and Mike each received 2 votes. Since Adam received the highest votes, we printed Adam and the number of votes.

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Auxiliary Space: O(n)
Constraints
  • 1 <= n <= 10^5
  • 1 <= |arri| <= 10^5
Companies
Adobe Atlassian
Topics
String Hashing
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions