Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package firstNonRecurringChar;
- import java.util.HashMap;
- public class test {
- public static void main(String[] args) {
- String s = "aabbcdde";
- HashMap hm = findFirst(s);
- for(int i = 0; i < s.length(); i++){
- if ((int) hm.get(s.charAt(i)) == 1){
- System.out.println("The first non-recurring character is: " + s.charAt(i));
- break;
- }
- }
- }
- public static HashMap findFirst(String s){
- HashMap hm = new HashMap();
- for (int i = 0; i < s.length(); i++){
- if (hm.get(s.charAt(i)) == null){
- hm.put(s.charAt(i), 1);
- }
- else {
- int previousValue = (int) hm.get(s.charAt(i));
- hm.put(s.charAt(i), previousValue + 1 );
- }
- }
- return hm;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement