Frequency of all NumbersWrite a Program to print all the frequencies in a given an Array.Constraints:First line consists an integer representing the length of the array(n).Length of the array must be greater than zero or else Print Invalid Input.Second line consists all the Elements of an array.All the Elements in the array must be greaterthan zero or else print Invalid Input.Example:Input 1 : 6 1 2 2 3 3 3Output 1 : 1 - 1 2 - 2 3 - 3Input 1 : -6Output 1 : Invalid Input
Question
Frequency of all NumbersWrite a Program to print all the frequencies in a given an Array.Constraints:First line consists an integer representing the length of the array(n).Length of the array must be greater than zero or else Print Invalid Input.Second line consists all the Elements of an array.All the Elements in the array must be greaterthan zero or else print Invalid Input.Example:Input 1 : 6 1 2 2 3 3 3Output 1 : 1 - 1 2 - 2 3 - 3Input 1 : -6Output 1 : Invalid Input
Solution
Here is a Python program that will solve the problem:
def print_frequencies():
n = int(input("Enter the length of the array: "))
if n <= 0:
print("Invalid Input")
return
arr = list(map(int, input("Enter the elements of the array: ").split()))
if any(i <= 0 for i in arr):
print("Invalid Input")
return
freq = {}
for i in arr:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
for key, value in freq.items():
print(f"{key} - {value}")
print_frequencies()
This program works as follows:
- It first takes the length of the array as input. If the length is less than or equal to 0, it prints "Invalid Input" and ends the program.
- It then takes the elements of the array as input. If any of the elements are less than or equal to 0, it prints "Invalid Input" and ends the program.
- It creates a dictionary to store the frequency of each number in the array.
- It then iterates over the array. For each number, it checks if it is already in the dictionary. If it is, it increments the frequency by 1. If it is not, it adds it to the dictionary with a frequency of 1.
- Finally, it prints the frequency of each number in the array.
Similar Questions
You are given an array nums consisting of positive integers.Return the total frequencies of elements in nums such that those elements all have the maximum frequency.The frequency of an element is the number of occurrences of that element in the array. Example 1:Input: nums = [1,2,2,3,1,4]Output: 4Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.So the number of elements in the array with maximum frequency is 4
You are given an array of integers. Sort them by frequency. See examples for more clarifications.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the elements of the array sorted by frequency. In case 2 elements have the same frequency, print the smaller element first.Constraints1 <= T <= 1001 <= N <= 10000-1000 <= A[i] <= 1000ExampleInput264 -2 10 12 -8 48176 -272 -272 -45 269 -327 -945 176Output-8 -2 10 12 4 4-945 -327 -45 269 -272 -272 176 176
Create a list of integers from user input. Write the C program to find the element that occurs with the highest frequency and also find the second-highest number in the given elements.Sample Input:512234Sample Output23
Read Array Size and ElementsWrite a Program to read the Array Size and Array Values from the User and Print Array Values?Constraints:Input :- First Line of Input Consists of One Integer Value (Array Size). Second Line of Input Consists of few Integer Values Separated by Spaces (Array Elements).Output :- Print the All Array Elements.Constraints :- NoExample:Input 1 : 8 86 210 33 73 53 93 848 48Output 1: 86 210 33 73 53 93 848 48 Input 2 : 6 1 2 3 4 5 6Output 2: 1 2 3 4 5 6 Input 3 : 5 5 10 88 2 5Output 3: 5 10 88 2 5
Compute the frequencycount of the following code fragment.for(i=0;i<n; i++)for(j=0;j<n; j++)printf(“%d”,a[i][j]);
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.