Java ProgramsArraysFind Least Frequent Element

Find Least Frequent Element in Java

intermediate·  Arrays  ·  Array

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.

Input
[4, 2, 4, 7, 2, 4]
Output
Least frequent element: 7 (1 time)

Java Program

Java
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

Least frequent element: 7 (1 time)

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.

How It Works
  1. 1A LinkedHashMap<Integer, Integer> named freq tracks each element's count, exactly like counting element frequency.
  2. 2freq.getOrDefault(num, 0) reads the current count, or 0 if unseen, then freq.put(num, ...) stores the incremented count.
  3. 3minElement and minCount start at the first entry's element and count, then walk the rest of entrySet().
  4. 4Whenever an entry's count is lower than minCount, both minElement and minCount are updated to that entry.
In [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.

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

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

LinkedHashMapgetOrDefault()running minimum

Approach 2: Java 8

Java
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

Least frequent element: 7 (1 time)

Core Logic

Once the frequency map exists, a stream can find its lowest-valued entry directly instead of a manual running-minimum 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 and counts each group, in first-appearance order.
  3. 3freq.entrySet().stream().min(Map.Entry.comparingByValue()) compares every entry by its count and keeps the single smallest one.
  4. 4.orElseThrow() unwraps the result from the Optional it's wrapped in.
Grouping [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.

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

Why: groupingBy() still visits every element once to build the map, and min() then makes one pass over its entries to find the smallest.

Key Concepts

StreamCollectors.groupingBy()Map.Entry.comparingByValue()

Related Programs