Java: Print a unique character in a string

前端 未结 17 1331
长情又很酷
长情又很酷 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条回答
  •  梦毁少年i
    2021-01-03 00:47

    The accepted answer will not pass all the test case for example

    input -"aaabcdd"

    desired output-"bc"
    but the accepted answer will give -abc

    because the character a present odd number of times.

    Here I have used ConcurrentHasMap to store character and the number of occurrences of character then removed the character if the occurrences is more than one time.

    import java.util.concurrent.ConcurrentHashMap;
    
    public class RemoveConductive {
    
        public static void main(String[] args) {
    
            String s="aabcddkkbghff";
    
            String[] cvrtar=s.trim().split("");
    
            ConcurrentHashMap hm=new ConcurrentHashMap<>();
            for(int i=0;i1){
                    hm.remove(ele);
                }
            }
            for(String key:hm.keySet()){
                System.out.print(key);
            }
        }  
    }
    

提交回复
热议问题