Java: Print a unique character in a string

前端 未结 17 1180
长情又很酷
长情又很酷 2021-01-03 00:03

I\'m writing a program that will print the unique character in a string (entered through a scanner). I\'ve created a method that tries to accomplish this but I keep getting

17条回答
  •  迷失自我
    2021-01-03 00:32

    This String algorithm is used to print unique characters in a string.It runs in O(n) runtime where n is the length of the string.It supports ASCII characters only.

    static String printUniqChar(String s) {
        StringBuilder buildUniq = new StringBuilder();
        boolean[] uniqCheck = new boolean[128];
        for (int i = 0; i < s.length(); i++) {
            if (!uniqCheck[s.charAt(i)]) {
                uniqCheck[s.charAt(i)] = true;
                if (uniqCheck[s.charAt(i)])
                    buildUniq.append(s.charAt(i));
            }
        }
    

提交回复
热议问题