BackeasyHashing PhonePe Swiggy

Majority Element Threshold Solution

Problem Statement

In a city election, votes are recorded for each candidate by ID. A dominant candidate is one who receives more than half of all votes. Find and return the dominant candidate's ID, or 'No dominant candidate' if no such candidate exists.

Example 1
Input
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Output
3

Explanation: Step-by-step: We iterate through the array and count the occurrences of each number. We find that 3 appears 10 times, which is more than half of the total votes (10). Therefore, the dominant candidate's ID is 3.

Example 2
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
No dominant candidate

Explanation: Step-by-step: We iterate through the array and count the occurrences of each number. We find that no number appears more than half of the total votes (10). Therefore, there is no dominant candidate.

Constraints

  • 1 <= n <= 5 * 10^4
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free