Question

Python Question: Create a program that prints the majority vote in a list. A majority vote...

Python Question:

Create a program that prints the majority vote in a list. A majority vote is an element that occurs > N/2 times in a list (where N is the length of the list).

Examples ["A", "A", "B"] ➞ "A"

["A", "A", "A", "B", "C", "A"] ➞ "A"

["A", "B", "B", "A", "C", "C"] ➞ None

Homework Answers

Answer #1

We can make a hashmap and keep the frequency of all the elements.

Then we can traverse the hashmap to find which element has frequency more than N/2.

# Python program for finding out majority 
# element in an array 
 
def findMajority(arr, size):
    m = {}
    for i in range(size):
        if arr[i] in m:
            m[arr[i]] += 1
        else:
            m[arr[i]] = 1
    count = 0
    for key in m:
        if m[key] > size / 2:
            count = 1
            print("Majority found :-",key)
            break
    if(count == 0):
        print("No Majority element")
 
# Driver code 
arr = [2, 2, 2, 2, 5, 5, 2, 3, 3] 
n = len(arr)
 
# Function calling 
findMajority(arr, n)

Please change the input in arr as required or you can make a for loop to provide user input capability.

please upvote if you find the answer helpful.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Question1: Write a Python Program that asks for the grades of 5 quizzes and then prints...
Question1: Write a Python Program that asks for the grades of 5 quizzes and then prints the highest grade. [6 Marks] Question2: Write a Python Program that does the following tasks. [8 Marks] Create an empty list Ask the user for 5 positive numbers and append them to the list Find and print the sum of all the numbers in the list Delete the last element from the list and then print the list Question3: Consider you have the following...
Write a python program that reads a word list and prints out all the anagrams in...
Write a python program that reads a word list and prints out all the anagrams in the word list. In earlier exercises you saw how to read a word list. Also, in earlier examples we saw how the sorted characters of a word are a useful canonical representation of an anagram (Hint: Therefore, useful as a key).
Write a Python program prints out the unique elements of a list lst in a sorted...
Write a Python program prints out the unique elements of a list lst in a sorted manner (i.e it removes duplicates from the list). For example, if list is [10,20,30,20,10,50,60,40,80,50,40], the output should be [10, 20, 30, 40, 50, 60, 80].?
Python recursive design question: Background: The class vote creates vote objects. A vote is either a...
Python recursive design question: Background: The class vote creates vote objects. A vote is either a blue vote, a red vote, or a purple. This information is stored in the "value" attribute. The function Poll returns a list of random votes. In the code below, some_poll is a list of 32 random votes. some_poll = Poll(32) # ----------------------------------------------------- import random # ----------------------------------------------------- # # Return a list of n random votes # # ----------------------------------------------------- def Poll(n=16): # # A random...
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
Question 1 ) Select all true statements about a Python slice of a list. (Wrong answers...
Question 1 ) Select all true statements about a Python slice of a list. (Wrong answers carry penalties.) Select one or more: a. It is created in Theta(1) time. b. It is created in Theta(n) time where n is the length of the list. c. It is a shallow copy of a fragment of the list. d. It is a deep copy of a fragment of the list. e. It is created in Theta(n) time where n is the length...
In Python You must create a flowchart, and its corresponding Python program, to solve the following...
In Python You must create a flowchart, and its corresponding Python program, to solve the following problem: 1. Using a repetition structure, evaluate the factorial of a positive whole number n: n! = n · (n - 1) · (n - 2) · ... · 1, where n >=1 Your program must take into account the cases in which the user enters an incorrect number and provide an error message for such cases.
Python Question Write the body of a function all_same_type(L) that consumes a list of any type...
Python Question Write the body of a function all_same_type(L) that consumes a list of any type and returns true if and only if each element in the list has the same type. Examples: all_same_type([]) => True all_same_type([2, 5, 3]) => True all_same_type([2, 'R', 4.56]) => False
Write a Python program to ask how many elements do users want to create in a...
Write a Python program to ask how many elements do users want to create in a list, then let the user create 2 lists with the number of elements previously entered. Then create and display all combinations of letters, selecting each letter from a different key in a dictionary. Example: How many elements? 2 List 1 = ['a', 'b'] List 2 = ['c', 'd'] Output: ac ad bc bd
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT