Programming Examples
Java program to accept n elements in array and find the frequency of any elements
Write a java program which accept n integer number in array and find the frequency of any input element.
Solution:
import java.util.*;
class Frequency
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of Array : ");
int n=sc.nextInt();
int num[]=new int[n];
int count=0;
System.out.println("Enter the "+n+" Elements of Array");
for(int i=0;i<n;i++)
{
num[i]=sc.nextInt();
}
System.out.println("Enter any number for Search : ");
int ch=sc.nextInt();
for(int i=0;i<n;i++)
{
if(num[i]==ch)
{
count++;
}
}
if(count>0)
{
System.out.println("Element Found "+count+" Time");
}
else
{
System.out.println("Element Not Found in array");
}
}
}
Output
Enter the size of Array :
5
Enter the 5 Elements of Array
22
11
22
23
22
Enter any number for Search :
22
Element Found 3 Time