Created
March 8, 2017 05:23
-
-
Save thmain/5534a5fdc07801f37d1d1daa0679c5c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
public class FirstRepeatingCharacter { | |
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); | |
if(map.containsKey(chr)){ | |
map.put(chr,map.get(chr)+1); | |
}else{ | |
map.put(chr, 1); | |
} | |
} | |
//Iterate the string and return the character for which the count is > 1 in map | |
for (int i = 0; i <input.length() ; i++) { | |
if(map.get(input.charAt(i))>1){ | |
rptChar = input.charAt(i); | |
break; | |
} | |
} | |
return rptChar; | |
} | |
public static void main(String[] args) { | |
String input = "horizon"; | |
Character result = getCharacter(input); | |
if(result!=null){ | |
System.out.println("First Repeating Character in '"+input+"' is: " + result); | |
}else{ | |
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