Java: Print a unique character in a string

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

    This is an interview question. Find Out all the unique characters of a string. Here is the complete solution. The code itself is self explanatory.

    public class Test12 {
        public static void main(String[] args) {
            String a = "ProtijayiGiniGina";
    
            allunique(a);
        }
    
        private static void allunique(String a) {
            int[] count = new int[256];// taking count of characters
            for (int i = 0; i < a.length(); i++) {
                char ch = a.charAt(i);
                count[ch]++;
            }
    
            for (int i = 0; i < a.length(); i++) {
                char chh = a.charAt(i);
                // character which has arrived only one time in the string will be printed out
                if (count[chh] == 1) {
                    System.out.println("index => " + i + " and unique character => " + a.charAt(i));
    
                }
            }
    
        }// unique
    
    }
    

    In Python :

    def firstUniqChar(a):
        count = [0] *256
        for i in a: count[ord(i)] += 1
        element = ""
    
        for item in a:
            if (count[ord(item)] == 1):
                element = item;
                break;
        return element        
    
    
    a = "GiniGinaProtijayi";
    print(firstUniqChar(a)) # output is P
    
    0 讨论(0)
  • 2021-01-03 00:30

    how about this :)

    for (int i=0; i< input.length();i++)
        if(input.indexOf(input.charAt(i)) == input.lastIndexOf(input.charAt(i)))
            System.out.println(input.charAt(i) + "  is unique");
    
    0 讨论(0)
  • 2021-01-03 00:31
    public void uniq(String inputString) {
        String result = "";
        int inputStringLen = inputStr.length();
        int[] repeatedCharacters = new int[inputStringLen];
        char inputTmpChar;
        char tmpChar;
    
        for (int i = 0; i < inputStringLen; i++) {
            inputTmpChar = inputStr.charAt(i);
            for (int j = 0; j < inputStringLen; j++) {
                tmpChar = inputStr.charAt(j);
                if (inputTmpChar == tmpChar)
                    repeatedCharacters[i]++;
            }
        }
    
        for (int k = 0; k < inputStringLen; k++) { 
            inputTmpChar = inputStr.charAt(k);
            if (repeatedCharacters[k] == 1)
                result = result + inputTmpChar + " ";
        }
    
        System.out.println ("Unique characters: " + result);
    }
    

    In first for loop I count the number of times the character repeats in the string.
    In the second line I am looking for characters repetitive once.

    0 讨论(0)
  • 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));
            }
        }
    
    0 讨论(0)
  • 2021-01-03 00:35

    Step1: To find the unique characters in a string, I have first taken the string from user. Step2: Converted the input string to charArray using built in function in java. Step3: Considered two HashSet (set1 for storing all characters even if it is getting repeated, set2 for storing only unique characters. Step4 : Run for loop over the array and check that if particular character is not there in set1 then add it to both set1 and set2. if that particular character is already there in set1 then add it to set1 again but remove it from set2.( This else part is useful when particular character is getting repeated odd number of times). Step5 : Now set2 will have only unique characters. Hence, just print that set2.

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String str = input.next();
        char arr[] = str.toCharArray();
        
        HashSet<Character> set1=new HashSet<Character>();
        HashSet<Character> set2=new HashSet<Character>();
    
        
        for(char i:arr)
        {
            if(set1.contains(i))
            {
                set1.add(i);
                set2.remove(i);
                
            }
            else
            {
                
                set1.add(i);
                set2.add(i);
            }
        }
        
        System.out.println(set2); 
    
    }
    
    0 讨论(0)
  • 2021-01-03 00:38
    import java.util.*;
    import java.lang.*;
    class Demo
    {
    public static void main(String[] args)
    {
    
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter String");
    String s1=sc.nextLine();
     try{
    HashSet<Object> h=new HashSet<Object>();
    for(int i=0;i<s1.length();i++)
    {
    h.add(s1.charAt(i));
    }
    Iterator<Object> itr=h.iterator();
      while(itr.hasNext()){
       System.out.println(itr.next());
        }
        }
        catch(Exception e)
        {
        System.out.println("error");
        }
    }
    }
    
    0 讨论(0)
提交回复
热议问题