- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 list of candidates who have got majority vote in python
Suppose we have a list of numbers called nums where each number represents a vote to a candidate. We have to find the ids of the candidates that have greater than floor(n/3) votes, in non-decreasing order.
So, if the input is like nums = [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7], then the output will be [6, 7], as 6 and 7 have 40% of the votes.
To solve this, we will follow these steps −
- ans := a new empty set
- sort the list nums
- i := 0
- n := size of nums
- while i < size of nums, do
- if occurrences of nums[i] in nums > (n / 3), then
- insert nums[i] into ans
- i := i + (n / 3)
- if occurrences of nums[i] in nums > (n / 3), then
- return ans in sorted order
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): ans = set([]) nums.sort() i = 0 n = len(nums) while i < len(nums): if nums.count(nums[i]) > n // 3: ans.add(nums[i]) i += n // 3 return sorted(list(ans)) ob = Solution() nums = [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7] print(ob.solve(nums))
Input
[3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7]
Output
[6, 7]
- Related Articles
- Program to find id of candidate who have hot majority vote in Python
- Majority Element 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?
- Program to find elements from list which have occurred at least k times in Python
- When are python objects candidates for garbage collection?
- Few toffees were distributed among Ramesh, Suresh and Prakash. Ramesh got 3/8, Suresh got 1/ 2 and Prakash got 1/8 toffees of the total toffees .Who got the maximum toffees?
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python program to extract only the numbers from a list which have some specific digits
- Program to find starting index of the child who receives last balloon in Python?
- Python program to find sum of elements in list
- C++ code to count who have declined invitation
- Check if all people can vote on two machines in Python
- MySQL Select to get users who have logged in today?
- Program to partition color list in Python
- Program to reverse a list by list slicing in Python

Advertisements