How to determine if a string has all unique characters in Java

1 Answer

0 votes
public class MyClass {
    static boolean count_unique_char(String s) {
        int[] ascii = new int[256];
        int size = s.length();
       
        for (int i = 0; i < size; i++) {
            ascii[s.charAt(i)] += 1;
            if (ascii[s.charAt(i)] > 1)
                return false;
        }
 
        return true;
    }
    public static void main(String args[]) {
        String s = "java python";

        System.out.println(count_unique_char(s));
    }
}

  
   
   
   
   
/*
run:
   
false
   
*/

 



answered May 7, 2022 by avibootz

Related questions

1 answer 130 views
1 answer 148 views
1 answer 119 views
1 answer 120 views
1 answer 104 views
...