Home  >  Forum  >  Core Java
Post New Query

calculate frequency of data in an array list



Asked on September 19, 2014
my code is
import java.util.*;
public class array
{
    public static void main(String args[])
    {
        int[] a=new int[10];
        Scanner sc=new Scanner(System.in);
        System.out.println("Please enter elements...");
        for(int j=0;j<10;j++)
            a[j]=sc.nextInt();
        System.out.println("Array elements are : ");
        for (int i=0;i<a.length;i++)
            System.out.println(a[i]);
    }
}

here we enter the 10 data in array list. please tell me how to calculate the frequency of data. please tell me the logic.. 



Replied on September 19, 2014
Check the solution

package com.gs.imd.am.dist.activator;
import java.util.Scanner;
public class NewJack
{
    public static void main(String args[])
    {
        int[] a=new int[10];
        Scanner sc=new Scanner(System.in);
        System.out.println("Please enter elements...");
        for(int j=0;j<10;j++)
            a[j]=sc.nextInt();
        System.out.println("Array elements are : ");
        for (int i=0;i<a.length;i++)
            System.out.println(a[i]);

        System.out.println("Please enter elements...");
        
        int numToCheckFreq = sc.nextInt();
        
        int cnt =0;
        for(int i=0; i< a.length; i++){
        if(a[i] ==numToCheckFreq ) {
        cnt++;
        }
        }
        
        System.out.println("---Number of frequency---Of "+ numToCheckFreq +" is "+cnt);
    }
}



Replied on September 19, 2014
thank u :)


Replied on September 19, 2014
please discribe this line int numToCheckFreq = sc.nextInt(); what is the use of this code?


Replied on September 20, 2014
int numToCheckFreq = sc.nextInt();

sc.nextInt(); will accept value from user and that value will be assigned to numToCheckFreq variable


Write Answer




©2019 concretepage.com | Privacy Policy | Contact Us