Counting unique characters in a String given by the user

前端 未结 12 1402
情书的邮戳
情书的邮戳 2021-02-15 17:42

I have to write a program that counts the uniques characters in a String given by the user. For example \"abc\" returns 3 and \"aabbccd\" returns 4. I am not allow to use advan

相关标签:
12条回答
  • 2021-02-15 18:16
    String myString = "";
    
    for(int i=0; i< word.length(); i++) {
        if(myString.indexOf(word.charAt(i)) == -1) {
            System.out.println(word.charAt(i));
            myString = myString + word.charAt(i);
        }
    }
    return myString.length();
    
    0 讨论(0)
  • 2021-02-15 18:18

    In case you are allowed to use Java Sets, The following code is readable, compact and doing the job smoothly

    public static int countUniqueChar(String word){
        Set<Character> wordSet = new HashSet<>();
        for(Character c : word.toCharArray())
            wordSet.add(c);
        return wordSet.size();
    }
    
    0 讨论(0)
  • 2021-02-15 18:18

    Try to see if the following code helps you:

    String myString = "";
    
    for(int i=0; i< word.length(); i++) {
    
        if(myString.indexOf(word.charAt(i)) == -1) {
            System.out.println(word.charAt(i));
            myString = myString + word.charAt(i);
        }
    }
    return myString.length();
    
    0 讨论(0)
  • 2021-02-15 18:19

    You can use HashSet collections for counting the unique elements in a string. It allow only unique element.

    Code Snippet

    public static int uniqueCount(String str) 
    {
       HashSet<Character> al = new HashSet<Character>();
       char[] arr= str.toCharArray();
       for (int i=0; i<arr.length; i++) 
       {
          al.add(arr[i]);  
       }
       return al.size() ;
    }  
    

    Refer this link to get full code: ideone

    0 讨论(0)
  • 2021-02-15 18:20
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter String:");
        int length = scanner.nextInt();
        char[] ch1 =  new char[length];
        String[] input = new String[length];
        for (int i = 0; i < length; i++) {
            String userInput = scanner.next();
            input[i] = userInput;
             ch1= userInput.toCharArray();
    
            Arrays.sort(ch1);
             System.out.println(ch1);
        }
    
    0 讨论(0)
  • 2021-02-15 18:22

    It is extremely easy :)

    public static int countUniqueCharacters(String input) {
        boolean[] isItThere = new boolean[Character.MAX_VALUE];
        for (int i = 0; i < input.length(); i++) {
            isItThere[input.charAt(i)] = true;
        }
    
        int count = 0;
        for (int i = 0; i < isItThere.length; i++) {
            if (isItThere[i] == true){
                count++;
            }
        }
    
        return count;
    }
    

    Example for input "aab"

    First for-cycle goes 3 times, each time for one char.

    Value of "a" is 97, so it turns isItThere[97] to true, then second "a" is involved, which is doing the same, isItThere[97] is set to true again (hence changing nothing).

    After that "b" is involved, value of char "b" is 98, therefore isItThere[98] is set to true.

    And then you have second for-cycle, where you cycle through the all isItThere array. If you find any true statement, you increment count. In our case, you find isItThere[97] and isItThere[98] as true statement, it means you increment twice and returning 2.

    0 讨论(0)
提交回复
热议问题