Advertisement
spmwilson

First non-recurring char

Mar 3rd, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package firstNonRecurringChar;
  2.  
  3. import java.util.HashMap;
  4.  
  5. public class test {
  6.        
  7.          public static void main(String[] args) {
  8.                
  9.                 String s = "aabbcdde";
  10.                 HashMap hm =  findFirst(s);
  11.                
  12.                 for(int i = 0; i < s.length(); i++){
  13.                         if ((int) hm.get(s.charAt(i)) == 1){
  14.                                 System.out.println("The first non-recurring character is: " + s.charAt(i));
  15.                                 break;
  16.                         }
  17.                 }
  18.                  
  19.         }
  20.  
  21.         public static HashMap findFirst(String s){
  22.                
  23.                 HashMap hm = new HashMap();
  24.                
  25.                 for (int i = 0; i < s.length(); i++){
  26.                         if (hm.get(s.charAt(i)) == null){
  27.                                 hm.put(s.charAt(i), 1);
  28.                         }
  29.                         else {
  30.                                 int previousValue = (int) hm.get(s.charAt(i));
  31.                                 hm.put(s.charAt(i), previousValue + 1 );
  32.                         }
  33.                 }
  34.                
  35.                
  36.                
  37.                 return hm;
  38.         }
  39.        
  40.        
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement