Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting unique characters in a String given by the user

Tags:

java

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 advanced Java classes like Map, Set, etc. I can only use arrays, Strings, for loops, while loops, if statements. I am trying to use a nested loop but am getting confused about how to write the algorithm for the second for loop.

public static int countUniqueCharacters(String input){

String orgInput = input.toLowerCase();
        int count = 0;
        int stringLength = input.length();
        for( int i = 0; i<stringLength; i++){
            for(int j = 2; j > j-i-1; j--){
                char temp = orgInput.charAt(i);
                if (temp == orgInput.charAt(j)){
                    count++;
like image 214
user3453347 Avatar asked Mar 23 '14 21:03

user3453347


People also ask

How to count unique characters in string in Java?

Write a Java program to count unique Characters in string. Given a string as input, write Java code to count and print the number of unique characters in String. If there are no unique characters in the string, the method returns -1 Input consists of a string.

How to count the number of repeating characters in a string?

(Hint: Unique characters are: H, W, A, R, E, Y, U, and other characters are repeating) Input a string. Call the getCounts () method with the input string.

How to increase the number of unique values in a string?

If the element in the string is not in the defined empty list (i.e. temp), we append that element to it and we can increase the number of unique values since it is the unique value in temp. If the same value is repeated we simply avoid it being appended to the list. Hence, our list (i.e. temp) consists of only unique values.

What happens if there are no unique characters in a string?

If there are no unique characters in the string, the method returns -1 Input consists of a string. The output consists of an integer. (Hint: Unique characters are: H, W, A, R, E, Y, U, and other characters are repeating) Input a string.


3 Answers

Using Java 8 you could do the following:

public static long countUniqueCharacters(String input) {
    return input.chars()
            .distinct()
            .count();
}

This creates an IntStream of chars, then takes only distincts values and then counts the number of occurences.

like image 77
skiwi Avatar answered Oct 13 '22 17:10

skiwi


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.

like image 8
libik Avatar answered Oct 13 '22 16:10

libik


Here another solution:

public static int countUniqueCharacters(String input) {
    String buffer = "";
    for (int i = 0; i < input.length(); i++) {
        if (!buffer.contains(String.valueOf(input.charAt(i)))) {
            buffer += input.charAt(i);
        }
    }
    return buffer.length();
}

The first occurance of each character is stored in buffer. Therefore you have of all characters one in buffer, therefore buffer.length() delivers the count you need.

like image 4
wumpz Avatar answered Oct 13 '22 18:10

wumpz