Write a program that takes a string and returns the number of unique characters in the string. If the given string does not contain any unique characters return 0. The return type of the output is the count of all unique characters in the strings.
Input and Output Format
- Input consists of a string.
- The output is an integer.
Refer sample output for formatting specifications
Sample Input 1:
HelloWorld
Sample Output 1:
5
Sample Input 2:
coco
Sample Output 2:
0
Java program to find unique characters in a string
Following are the steps to find the count of unique characters in a string:
- Input a string.
- Pass it to the StringBuffer(as it is mutable i.e it can be modified)
- Now, iterate over the string and initialize the count variable.
- Take another loop and compare each character with another using chartAt() method.
- If both the character is the same, call the deleteCharAt() method of the StringBuffer class. It would delete the charter at the specified position. After this, keep on decrementing the inner loop. Also, increment the count value.
- Now, check if the count value is greater than 1, then again delete char at the specified position and decrement the loop.
- At last, we will get the unique character in a string, just print the length of the string in a string buffer.
package com.company; import java.util.Scanner; public class CountUniqueCharacters { public static void main(String[] args) { System.out.println("Enter String :"); Scanner scanner = new Scanner(System.in); String s1 = scanner.nextLine(); StringBuffer sb = new StringBuffer(s1); for(int i = 0; i< sb.length();i++){ int count = 0; for(int j = i+1;j<sb.length();j++){ if(sb.charAt(i)==sb.charAt(j)){ sb.deleteCharAt(j); j--; count++; } } if(count >= 1){ sb.deleteCharAt(i); i--; } } System.out.println(sb.length()); } }
Output
Check Unique characters using HashSet
Write a code to check if the string contains a unique character or not using HashSet. Let us consider, a string “computer“, it contains all unique characters then the program should return true else false.
Input and Output Format
- Input is the string.
- Output is the boolean value.
Refer sample output for formatting specifications
Sample Input 1:
Computer
Sample Output 1:
true
Sample Input 2:
Hello world
Sample Output 2:
false
Program to Check Unique character in a string
HashSet allows only unique values so we will use the object of HashSet. Following are the steps to check unique characters using HashSet:
- Input a string from the user.
- Pass the string to checkUnique() method.
- Inside the method, create an object of HashSet. Iterate the whole string, and add each character one by one to the HashSet object.
- Print the value return by add method.
package com.demo2; import java.util.HashSet; import java.util.Scanner; public class UniqueCharacter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); checkUnique(input); } private static void checkUnique(String input) { boolean result = false; HashSet<Character> set = new HashSet<>(); for (int i =0 ;i < input.length();i++){ result = set.add(input.charAt(i)); if(result==false) break; } System.out.println(result); } }
Output
In this way, we learned how to find the count of unique characters in a given string.