check string have all unique characters?

To check String have all unique characters first we create HashSet object with characters type. HashSet do not allow duplicate value.
now we create one for loop and we iterate String data.convert this string data in characters type from s.charAt() method after that this characters to add in hashSet and set return type boolean.
inside if condition we check boolean condition value,if they return true then it is not unique else it is unique character.

					import java.util.HashSet;

public class UniqueChar {
	public static void main(String[] args) {
		String s = "tiem";
		boolean result = false;
		HashSet<Character> h = new HashSet<Character>();
		for (int i = 0; i < s.length(); i++) {
			result = h.add(s.charAt(i));
		if (result == false) {
			System.out.println("not unique character");
		System.out.println("String have unique character :- " + result);

Output :-
String have unique character :- true

Leave a Comment

Your email address will not be published. Required fields are marked *