Introduction
Finding the frequency of each element in an array is a common task that involves counting how many times each element appears in the array. This guide will walk you through writing a Java program that counts the frequency of elements in a given array.
Problem Statement
Create a Java program that:
- Prompts the user to enter the size of an array and its elements.
- Calculates the frequency of each element in the array.
- Displays the element along with its frequency.
Example:
- Input:
[1, 2, 2, 3, 4, 4, 4, 5]
- Output:
Element 1 occurs 1 time(s) Element 2 occurs 2 time(s) Element 3 occurs 1 time(s) Element 4 occurs 3 time(s) Element 5 occurs 1 time(s)
Solution Steps
- Read the Array Size and Elements: Use the
Scanner
class to take the size and elements of the array as input from the user. - Use a Data Structure to Track Frequencies: Utilize a
HashMap
to store each element as a key and its frequency as the value. - Traverse the Array: Iterate through the array and update the frequency of each element in the
HashMap
. - Display the Frequencies: Print each element along with its frequency.
Java Program
// Java Program to Find the Frequency of Elements in an Array // Author: https://www.rameshfadatare.com/ import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class FrequencyOfElements { public static void main(String[] args) { // Step 1: Read the size and elements of the array from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] array = new int[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i < size; i++) { array[i] = scanner.nextInt(); } // Step 2: Find the frequency of elements using a HashMap Map<Integer, Integer> frequencyMap = findFrequency(array); // Step 3: Display the frequency of each element System.out.println("Frequency of elements in the array:"); for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) { System.out.println("Element " + entry.getKey() + " occurs " + entry.getValue() + " time(s)"); } } // Method to find the frequency of elements in the array public static Map<Integer, Integer> findFrequency(int[] array) { Map<Integer, Integer> frequencyMap = new HashMap<>(); // Traverse the array and update the frequency map for (int element : array) { if (frequencyMap.containsKey(element)) { frequencyMap.put(element, frequencyMap.get(element) + 1); } else { frequencyMap.put(element, 1); } } return frequencyMap; } }
Explanation
Step 1: Read the Array Size and Elements
- The
Scanner
class is used to read the size of the array and its elements. ThenextInt()
method captures the size and each element.
Step 2: Use a Data Structure to Track Frequencies
- A
HashMap
is used to store each element as a key and its frequency as the value.
Step 3: Traverse the Array
- A
for
loop is used to iterate through each element of the array. - During each iteration:
- If the element already exists in the
HashMap
, its frequency is incremented by 1. - If the element does not exist in the
HashMap
, it is added with a frequency of 1.
- If the element already exists in the
Step 4: Display the Frequencies
- The program prints each element along with its frequency using a
for-each
loop that iterates over theHashMap
‘s entry set.
Output Example
Example 1:
Enter the size of the array: 8 Enter the elements of the array: 1 2 2 3 4 4 4 5 Frequency of elements in the array: Element 1 occurs 1 time(s) Element 2 occurs 2 time(s) Element 3 occurs 1 time(s) Element 4 occurs 3 time(s) Element 5 occurs 1 time(s)
Example 2:
Enter the size of the array: 5 Enter the elements of the array: 10 20 20 30 10 Frequency of elements in the array: Element 10 occurs 2 time(s) Element 20 occurs 2 time(s) Element 30 occurs 1 time(s)
Example 3:
Enter the size of the array: 6 Enter the elements of the array: 7 7 7 7 7 7 Frequency of elements in the array: Element 7 occurs 6 time(s)
Conclusion
This Java program demonstrates how to find the frequency of elements in an array using a HashMap
. The HashMap
efficiently tracks the frequency of each element, making this approach both simple and effective. This exercise is valuable for understanding how to use data structures like HashMap
in Java programming for counting occurrences and managing key-value pairs.