Find Least Frequent Element in Java
Problem
The least frequent element is whichever value has the lowest count once every element's occurrences have been tallied.
Given an array of integers, find the element that appears the fewest times, along with its count.
Java Program
import java.util.LinkedHashMap;
import java.util.Map;
public class LeastFrequentElement {
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);
}
Integer minElement = null;
int minCount = Integer.MAX_VALUE;
for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
if (entry.getValue() < minCount) { // new lowest count found
minElement = entry.getKey();
minCount = entry.getValue();
}
}
System.out.println("Least frequent element: " + minElement + " (" + minCount + " time)");
}
}Output
Core Logic
Building the frequency map first, then scanning it once for the lowest count, is the mirror image of finding the most frequent element.
- 1A
LinkedHashMap<Integer, Integer>namedfreqtracks each element's count, exactly like counting element frequency. - 2
freq.getOrDefault(num, 0)reads the current count, or0if unseen, thenfreq.put(num, ...)stores the incremented count. - 3
minElementandminCountstart at the first entry's element and count, then walk the rest ofentrySet(). - 4Whenever an entry's count is lower than
minCount, bothminElementandminCountare updated to that entry.
[4, 2, 4, 7, 2, 4], 7 appears just once — fewer than 4 or 2 — so it's reported as the least frequent element.Key Point: Starting minCount from the first entry, rather than from a sentinel like Integer.MAX_VALUE, guarantees a valid element is always reported, even for an array with only one distinct value.
Why: Building the frequency map visits every element once and 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 LeastFrequentElementStream {
public static void main(String[] args) {
int[] arr = {4, 2, 4, 7, 2, 4};
Map<Integer, Long> freq = Arrays.stream(arr).boxed()
.collect(Collectors.groupingBy(num -> num, LinkedHashMap::new, Collectors.counting()));
// Compares every entry by its count and keeps the smallest one
Map.Entry<Integer, Long> minEntry = freq.entrySet().stream()
.min(Map.Entry.comparingByValue())
.orElseThrow();
System.out.println("Least frequent element: " + minEntry.getKey() + " (" + minEntry.getValue() + " time)");
}
}
Output
Core Logic
Once the frequency map exists, a stream can find its lowest-valued entry directly instead of a manual running-minimum loop.
- 1
Arrays.stream(arr).boxed()turns theint[]into aStream<Integer>. - 2
Collectors.groupingBy(num -> num, LinkedHashMap::new, Collectors.counting())groups equal elements and counts each group, in first-appearance order. - 3
freq.entrySet().stream().min(Map.Entry.comparingByValue())compares every entry by its count and keeps the single smallest one. - 4
.orElseThrow()unwraps the result from theOptionalit's wrapped in.
[4, 2, 4, 7, 2, 4] produces counts for each element, and min() picks out 7: 1 as the smallest entry.Key Point: Using LinkedHashMap::new as the map factory keeps ties resolved the same way as the manual loop — min() keeps the first minimum it encounters while scanning entries in insertion order.
Why: groupingBy() still visits every element once to build the map, and min() then makes one pass over its entries to find the smallest.