Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import java.util.HashMap;
public class LastRepeatingCharacter {
public static Character getCharacter(String input){
//remove all the spaces
input = input.replaceAll(" ", "");
Character rptChar = null;
//Will store each character and it's count
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i <input.length(); i++) {
Character chr = input.charAt(i);
map.put(chr, 1);
//Iterate the string from end to start and return the character for which the count is > 1 in map
for (int i = input.length()-1; i >=0 ; i--) {
rptChar = input.charAt(i);
return rptChar;
public static void main(String[] args) {
String input = "tutorial horizon";
Character result = getCharacter(input);
System.out.println("Last Repeating Character in '"+input+"' is: " + result);
System.out.println("No Repeating Character found");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment