Program to calculate frequency of each element in Array

Program to calculate frequency of each element in Array

Java Program to calculate Total Number of each element in Array.

In this program,given an array of size ‘n’,Consist of m elements in it. We have to find frequency of each element in the array. i.e how many times a given element occurred in the array.

Array:-> 10,20,30,10,30,40,50,50,10,20
         Number        frequency(No of Times Element Occurred)
         10             3
         20             2
         30             2
         40             1
         50             2

From the above example it is now clear what we have to do in this program.

Algorithm/program Explanation

  1. Start
  2. Define two arrays
    • int array[] new int[100];
    • int frequency[]=new int [100];
  3. Using Scanner Class take the user defined Input to Array .
    • for(int i=0;i<size;i++)
    • array[i]=sc.nextInt();
  4. Define int variable i,j,size,count for further use.
  5. Define two for loop:
    • a.for(i=0;i<size;i++)
      • count=1;
    • b. for(j=i+1;j,size;J++)
      • b.1.Check for condition
        • if(array[i]==array[j]) then
        • count=count+1;
        • frequency[j]=0;
      • b.2. Check for condition
        • if (frequency[i] != 0)
        • frequency[i] = count;
  6. Using Print Statement print array[i] and frequency[i]
  7. end

Program to calculate the frequency of each element in Array

/*
Java Program to Calculate Frequency of Occurrence of Each elements in The Array */
import java.util.Scanner;
import java.util.Scanner;
 public class FrequencyOfArray {
   public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int array[] = new int[100];
    int frequency[] = new int[100];
    int size, i, j, count;
    System.out.println("Enter the Size of Array:");
    size = sc.nextInt();
    System.out.println("Enter the Elements in Array:");
    for (i = 0; i < size; i++) {
     array[i] = sc.nextInt();
     frequency[i] = -1;
    }
    for (i = 0; i < size; i++) {
     count = 1;
     for (j = i + 1; j < size; j++) {
      if (array[i] == array[j]) {
       count = count + 1;
       frequency[j] = 0;
      }
     }
     if (frequency[i] != 0) {
      frequency[i] = count;
     }
    }
    System.out.println("Frequency of Each Element in an Array are:");
    System.out.println("Number|Frequency");
    for(i=0;i<size;i++){
     if(frequency[i]!=0){

      System.out.println("    "+array[i]+   "|" + " " + frequency[i]);
     }
    }
   }


}

Enter the Size of Array:
10
Enter the Elements in Array:
10 20 10 30 40 50 20 30 50 40
Frequency of Each Element in an Array are:
Number|Frequency
10| 2
20| 2
30| 2
40| 2
50| 2

Question Asked in this topic

  • Given an integer array calculate frequency of occurrence of each element in the array?
  • Write a logic to count the frequency of each number in the array?
  • Take user defined array,write a logic to find frequency of occurrence of each element present in the array?

Click Here for more Java Programs

More Relevant topic!

Leave a Comment