Count frequency of each element of array | Java code

 Count frequency of each element of array | Java code



Code:

import java.util.*;

public class Frequency{

  public static void countFre(int arr[] ,int n){

   boolean visit[] = new boolean[n];

   for(int i = 0; i<n ;i++){

     if(visit[i] == true)

     continue;

     

     int count=1;

     for(int j = i+1;j<n;j++){

       if(arr[i] == arr[j]){

         visit[j] = true;

         count++;

       }

     }

     System.out.println(arr[i] + " " + count);

   }

  }

  public static void main (String [] args){

    int arr[] = {1,1,2,3,4,3,2,4,1,1,1,1,1};

    int n = arr.length;

    countFre(arr,n);

  }

}


Output:

1 7
2 2
3 2
4 2

1 comment: