
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find id of candidate who have hot majority vote in Python
Suppose we have a list of numbers called nums containing n values, where each number represents a vote to a candidate. We have to find the id of the candidate that has greater than floor(n/2) number of votes, and if there is no majority vote then return -1.
So, if the input is like nums = [6, 6, 2, 2, 3, 3, 3, 3, 3], then the output will be 3.
To solve this, we will follow these steps −
- l := size of nums
- count := a map containing each individual number and their occurrences
- for each number i and occurrence j in count, do
- if j > (l / 2), then
- return i
- if j > (l / 2), then
- return -1
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): l = len(nums) from collections import Counter count = Counter(nums) for i, j in count.items(): if j > (l // 2): return i return -1 ob = Solution() nums = [6, 6, 2, 2, 3, 3, 3, 3, 3] print(ob.solve(nums))
Input
[6, 6, 2, 2, 3, 3, 3, 3, 3]
Output
3
- Related Articles
- Program to list of candidates who have got majority vote in python
- Out of $15,000$ voters in a constituency, $60 \%$ voted. Find the percentage of voters who did not vote. Can you now find how many actually did not vote?
- Majority Element in Python
- Program to find starting index of the child who receives last balloon in Python?
- Program to find total amount of money we have in bank in Python
- Program to find minimum number of flips required to have alternating values in Python
- Program to find out the number of people who get a food packet using Python
- Write a program in Python to find the minimum age of an employee id and salary in a given DataFrame
- Python Program to extract email-id from URL text file
- Program to find out the buildings that have a better view in Python
- Check if all people can vote on two machines in Python
- Candidate Key in RDBMS
- Candidate Key in DBMS
- Program to find k where k elements have value at least k in Python
- id() function in Python

Advertisements