Last Duplicate Character in a String
EasyUpdated: Aug 19, 2025
Problem
Given a string, find the last character (from right) that appears more than once in the string (ignoring spaces). If no such character exists, return a suitable message.
Examples
Example 1
Input:
str = "horizon tutorials"
Output: 'i'
Explanation: 'i' is the last character (from right) that appears more than once.
Example 2
Input:
str = "algorithms"
Output: No repeating character found.
Explanation: Every character is unique.
Solution
Method 1 – Naive Double Loop
Intuition
Check each character from right to left, and for each, scan the rest of the string to see if it appears more than once.
Approach
- Iterate the string from right to left.
- For each character, check if it appears elsewhere in the string.
- Return the first such character found.
- If none found, return a suitable message.
Code
C++
class Solution {
public:
char lastRepeatingChar(const string& s) {
for (int i = s.size() - 1; i >= 0; --i) {
if (s[i] == ' ') continue;
for (int j = 0; j < s.size(); ++j) {
if (i != j && s[i] == s[j]) return s[i];
}
}
return '\0'; // or throw/return special value
}
};
Go
func lastRepeatingChar(s string) byte {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == ' ' { continue }
for j := 0; j < len(s); j++ {
if i != j && s[i] == s[j] { return s[i] }
}
}
return 0
}
Java
class Solution {
public Character lastRepeatingChar(String s) {
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == ' ') continue;
for (int j = 0; j < s.length(); j++) {
if (i != j && s.charAt(i) == s.charAt(j)) return s.charAt(i);
}
}
return null;
}
}
Python
class Solution:
def last_repeating_char(self, s: str) -> str | None:
for i in range(len(s) - 1, -1, -1):
if s[i] == ' ': continue
if s.count(s[i]) > 1:
return s[i]
return None
Complexity
- ⏰ Time complexity:
O(n^2), since for each character, we may scan the whole string. - 🧺 Space complexity:
O(1), no extra space beyond variables.
Method 2 – Hash Map for Counting
Intuition
Count the occurrences of each character, then scan from right to left to find the last character with count > 1.
Approach
- Remove spaces from the string.
- Count occurrences of each character using a hash map.
- Iterate from right to left, return the first character with count > 1.
- If none found, return a suitable message.
Code
C++
#include <unordered_map>
class Solution {
public:
char lastRepeatingChar(const string& s) {
unordered_map<char, int> cnt;
for (char c : s) if (c != ' ') cnt[c]++;
for (int i = s.size() - 1; i >= 0; --i) {
if (s[i] != ' ' && cnt[s[i]] > 1) return s[i];
}
return '\0';
}
};
Go
func lastRepeatingChar(s string) byte {
cnt := map[byte]int{}
for i := 0; i < len(s); i++ {
if s[i] != ' ' { cnt[s[i]]++ }
}
for i := len(s) - 1; i >= 0; i-- {
if s[i] != ' ' && cnt[s[i]] > 1 { return s[i] }
}
return 0
}
Java
import java.util.*;
class Solution {
public Character lastRepeatingChar(String s) {
Map<Character, Integer> cnt = new HashMap<>();
for (char c : s.toCharArray()) if (c != ' ') cnt.put(c, cnt.getOrDefault(c, 0) + 1);
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) != ' ' && cnt.get(s.charAt(i)) > 1) return s.charAt(i);
}
return null;
}
}
Python
class Solution:
def last_repeating_char(self, s: str) -> str | None:
from collections import Counter
cnt = Counter(c for c in s if c != ' ')
for i in range(len(s) - 1, -1, -1):
if s[i] != ' ' and cnt[s[i]] > 1:
return s[i]
return None
Complexity
- ⏰ Time complexity:
O(n), since we scan the string twice. - 🧺 Space complexity:
O(1)(if alphabet is fixed), otherwiseO(n)for the hash map.