Home  >  Forum  >  Core Java
Post New Query

calculate frequency of data in an array list


joined on
October 10,2013
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.. 
joined on
March 14,2013
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);
    }
}
joined on
October 10,2013
Replied on September 19,2014

thank u :)
joined on
October 10,2013
Replied on September 19,2014

please discribe this line int numToCheckFreq = sc.nextInt(); what is the use of this code?
joined on
March 14,2013
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









Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us