Write a program to print the frequency of element in an array.

For more programs
https://java4school.com/count-words-start-and-end-with-a-vowel

import java.io.*;
class frequency
{
 public static void main()throws IOException
 {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Enter the size of the array");
 int n=Integer.parseInt(br.readLine());
 int Arr[]=new int[n];
 int FREQ[]=new int[n];
 int visited=-1;
 System.out.println("Enter the values");
 for(int i=0;i<n;i++)
 {
 Arr[i]=Integer.parseInt(br.readLine());
 }
 for(int i=0;i<n;i++)
 {
 int count=1;
 for(int j=i+1;j<n;j++)
 {
 if(Arr[i]==Arr[j])
 {
 count ++;
 FREQ[j]=visited;
 }
 }
 if(FREQ[i]!=visited)
 {
 FREQ[i]=count;
 }
 }
 System.out.println("------------------------------------------");
 System.out.println("ELEMENT"+"\t"+"FREQUENCY");
 System.out.println("------------------------------------------");
 for(int i=0;i<n;i++)
 {
 if(FREQ[i]!=visited)
 {
 System.out.println(Arr[i]+"\t"+FREQ[i]);
 }
 }
 }
}

Output:
Enter the size of the array
4
10
20
10
15
ELEMENT FREQUENCY
10 2
20 1
15 1

For java videos click
https://www.youtube.com/user/trushna.tej

LEAVE A REPLY

Please enter your comment!
Please enter your name here