Java Program to find frequency of each element in Array

Java Program to find frequency of each element in Array

In this programming tutorial you will learn writing Java Program to find the frequency of each elements given in Array. To find the counting of each elements we can take a help of another array where we will store the frequency of each elements.

For example:

Given array arr = [1, 3, 3, 4, 2, 3, 3, 2, 6, 6]

So output will be like

1 -> 1

3 -> 4

4 -> 1

2 -> 2

6 -> 2

Program 1 : Find the frequency of array elements in Java

import java.util.Arrays;
import java.util.*; 
class Main
{
    public static void main(String []args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Java Program to count frequency of elements");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        } 
        boolean visited[] = new boolean[size];
        Arrays.fill(visited, false);
        for (int i = 0; i < size; i++) {
            if (visited[i] == true)
                continue;
            int count = 1;
            for (int j = i + 1; j < size; j++) {
                if (arr[i] == arr[j]) {
                    visited[j] = true;
                    count++;
                }
            }
            System.out.println(arr[i] + " " + count);
        }
    }
}

Output

Java Program to count frequency of elements
Enter the size of array: 5
Please give value for index 0 : 2
Please give value for index 1 : 3
Please give value for index 2 : 4
Please give value for index 3 : 4
Please give value for index 4 : 2
2 2
3 1
4 2

Program 2 : Find frequency of array elements using recursion

import java.util.Arrays;
import java.util.*; 
class Main
{
    public static void main(String []args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Java Program to count frequency of elements");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        } 
        Map<Integer, Integer>map = new HashMap<>();
        for (int i = 0; i <size; i++)
        {
            if (map.containsKey(arr[i]))
            {
                map.put(arr[i], map.get(arr[i]) + 1);
            }
            else
            {
                map.put(arr[i], 1);
            }
        }
        System.out.println("Frequency of array elements are : ");
        for (Map.Entry<Integer, Integer> entry : map.entrySet())
        {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}

Output

Java Program to count frequency of elements
Enter the size of array: 6
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 1
Please give value for index 3 : 3
Please give value for index 4 : 3
Please give value for index 5 : 3
Frequency of array elements are : 
1 2
2 1
3 3