Java Program to find frequency of a number in an array


In this program, You will learn how to find 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 frequency of a number in an array in Java

Run it Now
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("\nEnter Array Elements :");
        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }

        System.out.print("\nEnter Number for Frequency :");
        l = s.nextInt();

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

        System.out.println("\nNumber Of Frequency :" + k);
    }
}

Output:

Enter Size of an Array :5
Enter Array Elements :10 20 30 20 44
Enter Number for Frequency :20
Number Of Frequency :2