Find Frequency of Elements in Java
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.
Java Program
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
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.
- 1A
LinkedHashMap<Integer, Integer>namedfreqtracks each element's count, preserving the order elements were first seen. - 2
freq.getOrDefault(num, 0)reads the current count for an element, or0if it hasn't been seen yet. - 3Adding
1and callingfreq.put(num, ...)stores the updated count in one line. - 4
entrySet()is iterated afterward to print every element alongside its final count.
[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.
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
Approach 2: Java 8
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
Core Logic
groupingBy plus counting() builds the same frequency map declaratively, in one expression instead of a manual loop.
- 1
Arrays.stream(arr).boxed()turns theint[]into aStream<Integer>. - 2
Collectors.groupingBy(num -> num, LinkedHashMap::new, Collectors.counting())groups equal elements together and counts how many are in each group. - 3Passing
LinkedHashMap::newas the map factory keeps the result in first-appearance order, matching the manual version. - 4
freq.forEach((num, count) -> ...)prints every entry using a lambda instead of an explicitentrySet()loop.
[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.
Why: groupingBy() still visits every element once while building the map, which holds one entry per distinct value just like the manual version.