Finding the number of occurrences of each element in a Java array

When working with arrays in Java, it’s often necessary to find the number of occurrences of each element in the array. This can be useful in various scenarios, such as analyzing data or identifying the most commonly occurring elements.

Here’s an example of how you can find the number of occurrences of each element in a Java array:

import java.util.HashMap;
import java.util.Map;

public class ArrayElementCount {
    public static void main(String[] args) {
        int[] array = { 1, 2, 3, 4, 2, 1, 3, 2, 4, 4, 4 };

        Map<Integer, Integer> elementCount = new HashMap<>();

        for (int i = 0; i < array.length; i++) {
            if (elementCount.containsKey(array[i])) {
                elementCount.put(array[i], elementCount.get(array[i]) + 1);
            } else {
                elementCount.put(array[i], 1);
            }
        }

        for (Map.Entry<Integer, Integer> entry : elementCount.entrySet()) {
            System.out.println("Element " + entry.getKey() + " occurs " + entry.getValue() + " times");
        }
    }
}

In this code, we first declare an array array with some sample data. We then create a HashMap called elementCount to store the element as the key and its occurrences as the value.

Next, we iterate over the elements of the array using a for loop. For each element, we check if it already exists as a key in the elementCount map. If it does, we increment its value by one. If it doesn’t, we add it to the map with an initial occurrence count of one.

Finally, we iterate over the elementCount map using a for-each loop and print out each element along with its occurrence count.

When you run this code, it will output:

Element 1 occurs 2 times
Element 2 occurs 3 times
Element 3 occurs 2 times
Element 4 occurs 4 times

This shows the number of occurrences for each element in the array.

By using this approach, you can easily find the number of occurrences of each element in a Java array. This can be helpful for various tasks that involve analyzing and processing data.