(Count occurrences of numbers) Write a program that reads an unspecified number of integers and finds the ones that have the most occurrences. For example, if you enter 2 3 40 3 5 4 –3 3 3 2 0, the number 3 occurs most often. Enter all numbers in one line. If not one but several numbers have the most occurrences, all of them should be reported. For example, since 9 and 3 appear twice in the list 9 30 3 9 3 2 4, both occurrences should be reported.
Count occurrences of numbers Program code:
def findMostFrequentNums():
# Read integers from user input
numbers = input("Enter integer numbers: ").split()
# Create a dictionary to store the count of each number
counts = {}
# Iterate over the numbers and count their occurrences
for num in numbers:
num = int(num) # Convert the number from string to integer
if num in counts:
counts[num] += 1
else:
counts[num] = 1
# Find the maximum count
max_count = max(counts.values())
# Find the numbers with the maximum count
findNums = [num for num, count in counts.items() if count == max_count]
# Display the numbers with the maximum count
print("The following are that occurs most often:")
for num in findNums:
print(num, end=" ")
# Run the function
findMostFrequentNums()
Executed Output 1:
Enter integer numbers: 3 40 3 5 4 -3 3 3 2 0
The following are that occurs most often:
3
Executed Output 2:
Enter integer numbers: 3 40 3 5 4 -3 3 3 2 0
The following are that occurs most often:
3