Problem statement:- Program to Print lists occurring elements in an array.
Data requirement:-
Input Data:- arr,size
Output Data:-list_oc, list_v
Output Data:-list_oc, list_v
Additional Data:- i, j, max, max, freq
Program in C
Here is the source code of the C Program to Print lists occurring elements in an array.
Code:
#include<stdio.h>
#include<limits.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],j,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]]++;
int list_oc=9999,list_v=9999;
for(j=0;j<size;j++)
{
if(freq[arr[j]]<list_oc)
{
list_oc=freq[arr[j]];
list_v=arr[j];
}}
printf("\nThe list occurring Number %d occurs %d time.",list_v,list_oc);
}
#include<limits.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],j,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]]++;
int list_oc=9999,list_v=9999;
for(j=0;j<size;j++)
{
if(freq[arr[j]]<list_oc)
{
list_oc=freq[arr[j]];
list_v=arr[j];
}}
printf("\nThe list occurring Number %d occurs %d time.",list_v,list_oc);
}
Input/Output:
Input/Output:
Input/Output:
Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
4
7
6
7
The list occurring Number 6 occurs 1 time.
Enter the Element of the array:
4
4
7
6
7
The list occurring Number 6 occurs 1 time.
Program in C++
Here is the source code of the C++ Program to Print lists occurring elements 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]]++;
int list_oc=9999,list_v=9999;
for(j=0;j<size;j++)
{
if(freq[arr[j]]<list_oc)
{
list_oc=freq[arr[j]];
list_v=arr[j];
}}
cout<<"\nThe list occurring Number "<<list_v<<" occurs "<<list_oc<<" times.";
}
#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]]++;
int list_oc=9999,list_v=9999;
for(j=0;j<size;j++)
{
if(freq[arr[j]]<list_oc)
{
list_oc=freq[arr[j]];
list_v=arr[j];
}}
cout<<"\nThe list occurring Number "<<list_v<<" occurs "<<list_oc<<" times.";
}
Input/Output:
Enter the size of the array:4
Enter the Element of the array:
7
8
7
9
The list occurring Number 8 occurs 1 times.
Enter the Element of the array:
7
8
7
9
The list occurring Number 8 occurs 1 times.
Program in Java
Here is the source code of the Java Program to Print lists occurring elements in an array.
Code:
import java.util.Scanner;
public class p20 {
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]]++;
int list_oc=9999,list_v=9999;
for(j=0;j<size;j++)
{
if(freq[arr[j]]<list_oc)
{
list_oc=freq[arr[j]];
list_v=arr[j];
}}
System.out.println( "The list occurring Number "+list_v+" occurs "+list_oc+" times.");
sc.close();
}
}
public class p20 {
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]]++;
int list_oc=9999,list_v=9999;
for(j=0;j<size;j++)
{
if(freq[arr[j]]<list_oc)
{
list_oc=freq[arr[j]];
list_v=arr[j];
}}
System.out.println( "The list occurring Number "+list_v+" occurs "+list_oc+" times.");
sc.close();
}
}
Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
7
9
7
7
9
The list occurring Number 9 occurs 2 times.
5
Enter the Element of the array:
7
9
7
7
9
The list occurring Number 9 occurs 2 times.
Program in Python
Here is the source code of the Python Program to Print lists occurring elements 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
list_oc=9999
list_v=9999
for i in range(0, size):
if freq[arr[i]] < list_oc:
list_oc = freq[arr[i]]
list_v = arr[i]
print("The List occurring Number ",list_v," occurs ",list_oc," 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
list_oc=9999
list_v=9999
for i in range(0, size):
if freq[arr[i]] < list_oc:
list_oc = freq[arr[i]]
list_v = arr[i]
print("The List occurring Number ",list_v," occurs ",list_oc," times.")
Input/Output:
Enter the size of the array: 3
Enter the Element of the array:
9
9
44
The List occurring Number 44 occurs 1 times.
Enter the Element of the array:
9
9
44
The List occurring Number 44 occurs 1 times.
0 Comments
Please do not Enter any spam link in the comment box