Java: Print a unique character in a string

前端 未结 17 1176
长情又很酷
长情又很酷 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:39

    Based on your desired output you can replace each character already present with a blank character.

    public static void uniqueCharacters(String test){
      String temp = "";
      for(int i = 0; i < test.length(); i++){
          if (temp.indexOf(test.charAt(i)) == - 1){
             temp = temp + test.charAt(i);
          } else {
             temp.replace(String.valueOf(temp.charAt(i)), "");
          }
     }
    
    System.out.println(temp + " ");
    

    }

    0 讨论(0)
  • 2021-01-03 00:41

    If you don't want to use additional space:

        String abc="developer";
    
        System.out.println("The unique characters are-");
    
        for(int i=0;i<abc.length();i++)
        {
            for(int j=i+1;j<abc.length();j++)
            {
                if(abc.charAt(i)==abc.charAt(j))
                    abc=abc.replace(String.valueOf(abc.charAt(j))," ");
            }
        }   
        System.out.println(abc);
    

    Time complexity O(n^2) and no space.

    0 讨论(0)
  • 2021-01-03 00:42

    Though to approach a solution I would suggest you to try and use a better data structure and not just string. Yet, you can simply modify your logic to delete already existing duplicates using an else as follows :

    public static void uniqueCharacters(String test) {
            String temp = "";
            for (int i = 0; i < test.length(); i++) {
                char ch = test.charAt(i);
                if (temp.indexOf(ch) == -1) {
                    temp = temp + ch;
                } else {
                    temp.replace(String.valueOf(ch),""); // added this to your existing code
                }
            }
    
            System.out.println(temp + " ");
    
        }
    
    0 讨论(0)
  • 2021-01-03 00:45

    Based on your desired output, you have to replace a character that initially has been already added when it has a duplicated later, so:

    public static void uniqueCharacters(String test){
        String temp = "";
        for (int i = 0; i < test.length(); i++){
            char current = test.charAt(i);
            if (temp.indexOf(current) < 0){
                temp = temp + current;
            } else {
                temp = temp.replace(String.valueOf(current), "");
            }
        }
    
        System.out.println(temp + " ");
    
    }
    
    0 讨论(0)
  • 2021-01-03 00:45
    public class UniqueCharactersInString {
    
    
     public static void main(String []args){
    
        String input = "aabbcc";
        String output = uniqueString(input);
    
        System.out.println(output);
     }
    
     public static String uniqueString(String s){
         HashSet<Character> uniques = new HashSet<>();
         uniques.add(s.charAt(0));
         String out = "";
         out += s.charAt(0);
    
         for(int i =1; i < s.length(); i++){
             if(!uniques.contains(s.charAt(i))){
                 uniques.add(s.charAt(i));
                 out += s.charAt(i);
             }
         }
         return out;
     }
    }
    

    What would be the inneficiencies of this answer? How does it compare to other answers?

    0 讨论(0)
  • 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<String,Integer> hm=new ConcurrentHashMap<>();
            for(int i=0;i<cvrtar.length;i++){
                if(!hm.containsKey(cvrtar[i])){
                    hm.put(cvrtar[i],1);
                }
                else{
                     hm.put(cvrtar[i],hm.get(cvrtar[i])+1);
                }
            }
            for(String ele:hm.keySet()){
                if(hm.get(ele)>1){
                    hm.remove(ele);
                }
            }
            for(String key:hm.keySet()){
                System.out.print(key);
            }
        }  
    }
    
    0 讨论(0)
提交回复
热议问题