Java ProgramsArraysFind Frequency of Elements

Find Frequency of Elements in Java

intermediate·  Arrays  ·  Array

Problem

Element frequency means counting how many times each individual value appears in an array.

Given an array of integers, count how many times each element appears.

Input
[4, 2, 4, 7, 2, 4]
Output
4: 3, 2: 2, 7: 1

Java Program

Java
import java.util.LinkedHashMap; import java.util.Map; public class FrequencyOfElements { public static void main(String[] args) { int[] arr = {4, 2, 4, 7, 2, 4}; Map<Integer, Integer> freq = new LinkedHashMap<>(); for (int num : arr) { // getOrDefault(num, 0) reads the current count, or 0 if unseen, then increments it freq.put(num, freq.getOrDefault(num, 0) + 1); } for (Map.Entry<Integer, Integer> entry : freq.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } }

Output

4: 3 2: 2 7: 1

Core Logic

A map tracking each element's running count, updated in one pass, is all it takes — the same idea as counting character frequency, just for numbers instead of letters.

How It Works
  1. 1A LinkedHashMap<Integer, Integer> named freq tracks each element's count, preserving the order elements were first seen.
  2. 2freq.getOrDefault(num, 0) reads the current count for an element, or 0 if it hasn't been seen yet.
  3. 3Adding 1 and calling freq.put(num, ...) stores the updated count in one line.
  4. 4entrySet() is iterated afterward to print every element alongside its final count.
In [4, 2, 4, 7, 2, 4], 4 ends with a count of 3, 2 ends with a count of 2, and 7 ends with a count of 1.
💡

Key Point: A plain HashMap would give the same counts but in an unpredictable order — LinkedHashMap is what keeps the output in first-appearance order.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each element is visited once to update its count, and the map holds one entry per distinct value, up to n in the worst case.

Key Concepts

LinkedHashMapgetOrDefault()entrySet()

Approach 2: Java 8

Java
import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; public class FrequencyOfElementsStream { public static void main(String[] args) { int[] arr = {4, 2, 4, 7, 2, 4}; // Groups equal elements together and counts how many are in each group Map<Integer, Long> freq = Arrays.stream(arr).boxed() .collect(Collectors.groupingBy(num -> num, LinkedHashMap::new, Collectors.counting())); freq.forEach((num, count) -> System.out.println(num + ": " + count)); } }

Output

4: 3 2: 2 7: 1

Core Logic

groupingBy plus counting() builds the same frequency map declaratively, in one expression instead of a manual loop.

How It Works
  1. 1Arrays.stream(arr).boxed() turns the int[] into a Stream<Integer>.
  2. 2Collectors.groupingBy(num -> num, LinkedHashMap::new, Collectors.counting()) groups equal elements together and counts how many are in each group.
  3. 3Passing LinkedHashMap::new as the map factory keeps the result in first-appearance order, matching the manual version.
  4. 4freq.forEach((num, count) -> ...) prints every entry using a lambda instead of an explicit entrySet() loop.
Grouping [4, 2, 4, 7, 2, 4] produces the same counts as the manual version: 4: 3, 2: 2, 7: 1.
💡

Key Point: groupingBy's three-argument form lets you choose the resulting map type — without LinkedHashMap::new, the default HashMap wouldn't guarantee this ordering.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: groupingBy() still visits every element once while building the map, which holds one entry per distinct value just like the manual version.

Key Concepts

StreamCollectors.groupingBy()Collectors.counting()

Related Programs