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