to find every element frequency..........

Write a java program to check and display frequency of each element :--


import java.util.Scanner;
public class FrequencyElements {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
int arr[]=inputArray();
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; 
        }   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("---------------------"); 
    } 
static int[] inputArray() {
System.out.println("Enter Size of Array : ");
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println("enter value in array : ");
for (int i=0; i<n; i++) {
arr[i]=sc.nextInt();
}
return arr;
}


}

Comments

Popular posts from this blog