Advertisement
zoltanvi

First repeated character in string (2)

Jul 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         System.out.println(firstRepeatedChar("dictionary"));    // i
  6.         System.out.println(firstRepeatedChar("abcdefgh"));      // null
  7.     }
  8.  
  9.     public static char firstRepeatedChar(String str){
  10.         if(str.length() < 2){
  11.             return '\0';
  12.         }
  13.         String s = str.toLowerCase();  // Optional
  14.  
  15.         boolean[] dictionary = new boolean[256];
  16.         for (int i = 0; i < s.length(); i++) {
  17.             char current = s.charAt(i);
  18.             if(!dictionary[current]){
  19.                 dictionary[current] = true;
  20.             } else {
  21.                 return current;
  22.             }
  23.         }
  24.         return '\0';
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement