Problem statement:- Program to Print the frequency of all numbers in an array.
Data requirement:-
Input Data:- arr,size
Output Data:-freq
Output Data:-freq
Additional Data:- i, j, max
Program in C
Here is the source code of the C Program to Print the frequency of all numbers in an array.
Code:
#include<stdio.h>
#include<limits.h>
int main()
{
printf("Enter the size of the array:");
int size,j;
scanf("%d",&size);
int arr[size],max=INT_MIN;
printf("Enter the Element of the array:\n");
for(j=0;j<size;j++)
{
scanf("%d",&arr[j]);
if(arr[j]>=max)
max=arr[j];
}
int freq[max+1];
for(j=0;j<max+1;j++)
{
freq[j]=0;
}
for(j=0;j<size;j++)
freq[arr[j]]++;
for(j=0;j<max+1;j++)
{
if(freq[j]!=0)
printf("occurs %d %d times\n",j,freq[j]);
}
}
#include<limits.h>
int main()
{
printf("Enter the size of the array:");
int size,j;
scanf("%d",&size);
int arr[size],max=INT_MIN;
printf("Enter the Element of the array:\n");
for(j=0;j<size;j++)
{
scanf("%d",&arr[j]);
if(arr[j]>=max)
max=arr[j];
}
int freq[max+1];
for(j=0;j<max+1;j++)
{
freq[j]=0;
}
for(j=0;j<size;j++)
freq[arr[j]]++;
for(j=0;j<max+1;j++)
{
if(freq[j]!=0)
printf("occurs %d %d times\n",j,freq[j]);
}
}
Input/Output:
Input/Output:
Input/Output:
Input/Output:
Enter the size of the array:5
Enter the Element of the array:
2
3
2
6
2
occurs 2 3 times
occurs 3 1 times
occurs 6 1 times
Enter the Element of the array:
2
3
2
6
2
occurs 2 3 times
occurs 3 1 times
occurs 6 1 times
Program in C++
Here is the source code of the C++ Program to Print the frequency of all numbers in an array.
Code:
#include<iostream>
#include<climits>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size,j;
cin>>size;
int arr[size],max=INT_MIN;
cout<<"Enter the Element of the array:\n";
for(j=0;j<size;j++)
{
cin>>arr[j];
if(arr[j]>=max)
max=arr[j];
}
int freq[max+1];
for(j=0;j<max+1;j++)
{
freq[j]=0;
}
for(j=0;j<size;j++)
freq[arr[j]]++;
for(j=0;j<max+1;j++)
{
if(freq[j]!=0)
cout<<"occurs "<<j<<" "<<freq[j]<<" times\n";
}
}
#include<climits>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size,j;
cin>>size;
int arr[size],max=INT_MIN;
cout<<"Enter the Element of the array:\n";
for(j=0;j<size;j++)
{
cin>>arr[j];
if(arr[j]>=max)
max=arr[j];
}
int freq[max+1];
for(j=0;j<max+1;j++)
{
freq[j]=0;
}
for(j=0;j<size;j++)
freq[arr[j]]++;
for(j=0;j<max+1;j++)
{
if(freq[j]!=0)
cout<<"occurs "<<j<<" "<<freq[j]<<" times\n";
}
}
Input/Output:
Enter the size of the array:6
Enter the Element of the array:
3
5
3
4
4
6
occurs 3 2 times
occurs 4 2 times
occurs 5 1 times
occurs 6 1 times
Enter the Element of the array:
3
5
3
4
4
6
occurs 3 2 times
occurs 4 2 times
occurs 5 1 times
occurs 6 1 times
Program in Java
Here is the source code of the Java Program to Print the frequency of all numbers in an array.
Code:
import java.util.Scanner;
public class p16 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size,j;
int max=Integer.MIN_VALUE;
size=sc.nextInt();
int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
for(j=0;j<size;j++)
{
arr[j]=sc.nextInt();
if(arr[j]>=max)
max=arr[j];
}
int freq[]=new int[max+1];
for(j=0;j<size;j++)
freq[arr[j]]++;
for(j=0;j<freq.length;j++)
{
if(freq[j]!=0)
System.out.println("occurs "+j+" "+freq[j]+ " times");
}
sc.close();
}
}
public class p16 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size,j;
int max=Integer.MIN_VALUE;
size=sc.nextInt();
int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
for(j=0;j<size;j++)
{
arr[j]=sc.nextInt();
if(arr[j]>=max)
max=arr[j];
}
int freq[]=new int[max+1];
for(j=0;j<size;j++)
freq[arr[j]]++;
for(j=0;j<freq.length;j++)
{
if(freq[j]!=0)
System.out.println("occurs "+j+" "+freq[j]+ " times");
}
sc.close();
}
}
Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
2
2
7
7
occurs 2 2 times
occurs 7 2 times
4
Enter the Element of the array:
2
2
7
7
occurs 2 2 times
occurs 7 2 times
Program in Python
Here is the source code of the Python Program to Print the frequency of all numbers in an array.
Code:
import sys
arr=[]
freq=[]
max=-sys.maxsize-1
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
for i in range(0, size):
if(arr[i]>=max):
max=arr[i]
for i in range(0,max+1):
freq.append(0)
for i in range(0, size):
freq[arr[i]]+=1
for i in range(0, max+1):
if(freq[i]!=0):
print("occurs ",i," ",freq[i]," times")
arr=[]
freq=[]
max=-sys.maxsize-1
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
for i in range(0, size):
if(arr[i]>=max):
max=arr[i]
for i in range(0,max+1):
freq.append(0)
for i in range(0, size):
freq[arr[i]]+=1
for i in range(0, max+1):
if(freq[i]!=0):
print("occurs ",i," ",freq[i]," times")
Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
6
7
2
2
1
occurs 1 1 times
occurs 2 2 times
occurs 6 1 times
occurs 7 1 times
Enter the Element of the array:
6
7
2
2
1
occurs 1 1 times
occurs 2 2 times
occurs 6 1 times
occurs 7 1 times
0 Comments
Please do not Enter any spam link in the comment box