How to check if String has all unique characters in java

In this post, we will see if String has all unique characters or not.

There are multiple ways to find if String has all unique characters or not.

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.arpit.java2blog;

public class StringAllUniqueCharMain {

 public static void main(String[] args) {
  
  System.out.println("java2blog has all unique chars : "+ hasAllUniqueChars("java2blog"));
  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 above program, you will get below output:
java2blog 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 returns 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.arpit.java2blog;

public class StringAllUniqueCharMain {

 public static void main(String[] args) {
  
  System.out.println("java2blog has all unique chars : "+ hasAllUniqueChars("java2blog"));
  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 above program, you will get below output:
java2blog 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.arpit.java2blog;

public class StringAllUniqueCharMain {

 public static void main(String[] args) {
  
  System.out.println("java2blog has all unique chars : "+ hasAllUniqueChars("java2blog"));
  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 above program, you will get below output:
java2blog has all unique chars : false
Apple has all unique chars : false
index has all unique chars : true
world has all unique chars : true


Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012