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++;
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.
(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.
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.
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.
Using Java 8 you could do the following:
public static long countUniqueCharacters(String input) {
return input.chars()
.distinct()
.count();
}
This creates an IntStream
of char
s, then takes only distincts values and then counts the number of occurences.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With