
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Program to list of candidates who have got majority vote in python
- 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
- C++ code to count who have declined invitation
- 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
- MySQL Select to get users who have logged in today?
- Hot Standby Mode
- C++ program to find indices of soldiers who can form reconnaissance unit
- Candidate Key in RDBMS
- Program to find out the buildings that have a better view in Python
- Find all documents that have two specific id's in an array of objects in MongoDB?
- Program to find k where k elements have value at least k in Python
- Majority Element in C++
Advertisements