Java ProgramsStringsFind Last Repeated Character in a String

Find Last Repeated Character in a String in Java

intermediate·  Strings  ·  String

Problem

A repeated character is one that appears two or more times in a string — the last one is whichever such character sits closest to the end.

Given a string, find the last character, reading right to left, that appears more than once.

Input
bookkeeper
Output
Last repeated character: e

Java Program

Java
import java.util.HashMap; import java.util.Map; public class LastRepeatedCharacter { public static void main(String[] args) { String str = "bookkeeper"; Map<Character, Integer> freq = new HashMap<>(); for (char c : str.toCharArray()) { freq.put(c, freq.getOrDefault(c, 0) + 1); } char result = '\0'; for (int i = str.length() - 1; i >= 0; i--) { char c = str.charAt(i); if (freq.get(c) > 1) { result = c; break; // first repeated character found scanning from the end } } System.out.println("Last repeated character: " + result); } }

Output

Last repeated character: e

Core Logic

Counting every character's total frequency first, then scanning the string backward, finds the last position holding a character that repeats.

How It Works
  1. 1A HashMap<Character, Integer> named freq tracks each character's total count across the whole string.
  2. 2freq.getOrDefault(c, 0) reads the current count, or 0 if unseen, then freq.put(c, ...) stores the incremented count.
  3. 3A second loop walks the string backward, from index str.length() - 1 down to 0.
  4. 4The first character encountered in that backward walk whose freq.get(c) is greater than 1 is the answer, printed immediately with break.
In "bookkeeper", the last character 'r' has a count of just 1, so the scan continues backward until it reaches 'e', which has a count of 3.
💡

Key Point: This is the mirror image of finding the first repeated character — same frequency map, but the second scan runs backward instead of forward.

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

Why: The first pass builds a frequency map holding one entry per distinct character, and the second pass re-scans the string backward looking up each character's count.

Key Concepts

HashMapgetOrDefault()reverse scan

Approach 2: Java 8

Java
import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; public class LastRepeatedCharacterStream { public static void main(String[] args) { String str = "bookkeeper"; Map<Character, Long> freq = str.chars() .mapToObj(c -> (char) c) .collect(Collectors.groupingBy(c -> c, Collectors.counting())); // Walks the string's indices in reverse, stopping at the first repeated character char result = IntStream.range(0, str.length()) .map(i -> str.length() - 1 - i) .mapToObj(str::charAt) .filter(c -> freq.get(c) > 1) .findFirst() .orElseThrow(); System.out.println("Last repeated character: " + result); } }

Output

Last repeated character: e

Core Logic

The same mirrored-index trick used to reverse an array or string can walk the string's indices backward as a stream, stopping at the first repeated character it finds.

How It Works
  1. 1str.chars().mapToObj(c -> (char) c) and Collectors.groupingBy() build the frequency map exactly like the manual version.
  2. 2IntStream.range(0, str.length()).map(i -> str.length() - 1 - i) generates the string's indices in reverse order.
  3. 3.mapToObj(str::charAt) converts each reversed index into its character.
  4. 4.filter(c -> freq.get(c) > 1).findFirst() keeps only repeated characters and takes the first one found in this backward order.
Walking "bookkeeper"'s indices in reverse visits 'r' first (count 1, skipped), then 'e' (count 3, matched).
💡

Key Point: IntStream.range(...).map(i -> length - 1 - i) is the same reversed-index pattern used elsewhere on this site to walk a sequence backward without building a separate reversed copy first.

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

Why: groupingBy() still visits every character once to build the map, and the reversed-index stream then makes one more pass looking for the first repeated character.

Key Concepts

StreamIntStreamfilter()findFirst()

Related Programs