Monday, May 2, 2022

Question 3 : Write a program to check if String has all unique characters in java?

 Solution: Here are some ways to check if a String contains all unique characters

  • By using HashSet
  • Using indexOf and lastIndexOf methods of String
  • By using ASCII values of characters.
  • Using Java 8

By Using HashSet:

  1. You can add each character to HashSet
  2. If HashSet’s add method returns false then it does not have all unique characters.

Java Program to check if String has all unique characters Using HashSet::

package org.CloudTechtwitter; public class StringAllUniqueCharMain { public static void main(String[] args) { System.out.println("CloudTechtwitter has all unique chars : "+ hasAllUniqueChars("CloudTechtwitter")); System.out.println("Apple has all unique chars : "+ hasAllUniqueChars("apple")); System.out.println("index has all unique chars : "+ hasAllUniqueChars("index")); System.out.println("world has all unique chars : "+ hasAllUniqueChars("world")); } public static boolean hasAllUniqueChars (String word) { HashSet alphaSet=new HashSet(); for(int index=0;index < word.length(); index ++) { char c =word.charAt(index); // If Hashset's add method return false,that means it is already present in HashSet if(!alphaSet.add(c)) return false; } return true; } }

When you run the above program, you will get the below output::
CloudTechtwitter has all unique chars : false Apple has all unique chars : false index has all unique chars : true world has all unique chars : true

By using indexOf and lastIndexOf methods.

If indexOf and lastIndexOf return the same value for the character, then it is not repeated in that String.

Java Program to check if String has all unique characters Using indexOf and lastIndexOf::

package org.CloudTechtwitter; public class StringAllUniqueCharMain { public static void main(String[] args) { System.out.println("CloudTechtwitter has all unique chars : "+ hasAllUniqueChars("CloudTechtwitter")); System.out.println("Apple has all unique chars : "+ hasAllUniqueChars("apple")); System.out.println("index has all unique chars : "+ hasAllUniqueChars("index")); System.out.println("world has all unique chars : "+ hasAllUniqueChars("world")); } public static boolean hasAllUniqueChars (String word) { for(int index=0;index < word.length(); index ++) { char c =word.charAt(index); if(word.indexOf(c)!=word.lastIndexOf(c)) return false; } return true; } }
 

When you run the above program, you will get the below output:
CloudTechtwitter has all unique chars : false Apple has all unique chars : false index has all unique chars : true world has all unique chars : true

By using ascii value of character

It is most efficient of all.
Approach:

  1. Create a boolean array of 26 length
  2. Convert char to uppercase and get its ascii value
  3. Subtract 64 to ascii value to get index between 0 to 25.
  4. If character is not repeated then we should have false in the boolean array

Java Program to check if String has all unique characters::

package org.CloudTechtwitter; public class StringAllUniqueCharMain { public static void main(String[] args) { System.out.println("CloudTechtwitter has all unique chars : "+ hasAllUniqueChars("CloudTechtwitter")); System.out.println("Apple has all unique chars : "+ hasAllUniqueChars("apple")); System.out.println("index has all unique chars : "+ hasAllUniqueChars("index")); System.out.println("world has all unique chars : "+ hasAllUniqueChars("world")); } public static boolean hasAllUniqueChars (String word) { boolean[] charMap = new boolean[26]; for(int index=0;index < word.length(); index ++) { // we are substracting char's ascii value to 64, so we get all index // from 0 to 25. int asciiCode = (int) word.toUpperCase().charAt(index) - 64; // If char is not present, it should have false at that index if(!charMap[asciiCode]) charMap[asciiCode] = true; else return false; } return true; } }

When you run the above program, you will get the below output
CloudTechtwitter has all unique chars : false Apple has all unique chars : false index has all unique chars : true world has all unique chars : true
Using Java 8
import java.util.Arrays; import java.util.Collections; import java.util.stream.Collectors; public class StringAllUniqueCharMain { public static void main(String[] args) { String word ="CloudTechtwitter" ; boolean value = word.chars(). filter(e-> Collections.frequency(word.chars().boxed(). collect(Collectors.toList()), e) > 1).count() > 1 ? false: true; System.out.println("The String has unique characters ? " + value); } }

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS