Java Program to find the frequency of each element in the array

In this program, the objective is to count the occurrence of each element in an array. One approach to solve this problem is to utilize an additional array to store the counts of each element from the original array. By looping through the array, we can calculate the frequency or occurrence of each element and store it in the corresponding position of another array, typically called “fr”.

1    2   8  3   2   2   2   5   1 

The array provided contains certain numbers, each appearing a specific number of times. For example, the number 1 has appeared twice, resulting in a frequency of 2. Similarly, the number 2 has appeared four times, giving it a frequency of 4. This pattern continues for other numbers in the array.

Algorithm of a Java Program to find the frequency of each element in the array

STEP 1: Begin

STEP 2: Initialize the array arr[] with values {1, 2, 8, 3, 2, 2, 2, 5, 1}.

STEP 3: Create an array fr[] of the same length as arr[].

STEP 4: Set the variable “visited” to -1.

STEP 5: Repeat STEP 6 to STEP 9 for each index i from 0 to the length of arr[].

STEP 6: Set the variable “count” to 1.

STEP 7: Repeat STEP 8 for each index j from i+1 to the length of arr[].

STEP 8: If the value at arr[i] is equal to the value at arr[j], then: – Increment the count by 1. – Set the value at fr[j] to “visited”.

STEP 9: If the value at fr[i] is not equal to “visited”, then: – Set the value at fr[i] to the current count.

STEP 10: Print “————“.

STEP 11: Print “Element | Frequency”.

STEP 12: Print “————-“.

STEP 13: Repeat STEP 14 for each index i from 0 to the length of fr[].

STEP 14: If the value at fr[i] is not equal to “visited”, then: – Print the value at arr[i] and the value at fr[i].

STEP 15: Print “————-“.

STEP 16: End.

Java Program to find the frequency of each element in the array

public class Frequency {  
    public static void main(String[] args) {  
        //Initialize array  
        int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};  
        //Array fr will store frequencies of element  
        int [] fr = new int [arr.length];  
        int visited = -1;  
        for(int i = 0; i < arr.length; i++){  
            int count = 1;  
            for(int j = i+1; j < arr.length; j++){  
                if(arr[i] == arr[j]){  
                    count++;  
                    //To avoid counting same element again  
                    fr[j] = visited;  
                }  
            }  
            if(fr[i] != visited)  
                fr[i] = count;  
        }  
  
        //Displays the frequency of each element present in array  
        System.out.println("---------------------------------------");  
        System.out.println(" Element | Frequency");  
        System.out.println("---------------------------------------");  
        for(int i = 0; i < fr.length; i++){  
            if(fr[i] != visited)  
                System.out.println("    " + arr[i] + "    |    " + fr[i]);  
        }  
        System.out.println("----------------------------------------");  
    }}

Output:

---------------------------------------
 Element | Frequency
---------------------------------------
    1    |    2
    2    |    4
    8    |    1
    3    |    1
    5    |    1
----------------------------------------

In conclusion, the Java program provided aims to determine the frequency of each element present in an array. It follows a step-by-step approach to achieving this task. The program initializes an array with given values, creates another array to store the frequencies, and uses a nested loop to compare elements and count their occurrences. Follow tutorials.freshersnow.com to learn more.