Java Program to find the frequency of a number in an array


In this program, You will learn how to find the frequency of a number in an array in java.


List is: 1 44 3 4 5 44

Number of 44 frequency is: 2

Example: How to find the frequency of a number in an array in java.

import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        int a[] = new int[100];
        int n, l, k = 0;

        System.out.print("Enter size of an array:");
        n = s.nextInt();

        System.out.print("Enter array elements:");
        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }

        System.out.print("Enter number for check frequency :");
        l = s.nextInt();

        for (int i = 0; i < n; i++) {
            if (l == a[i]) {
                k++;
            }
        }

        System.out.println("Number of frequency:" + k);
    }
}

Output:

Enter size of an array:5
Enter array elements:10 20 30 20 30
Enter number for check frequency :20
Number of frequency:2

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.