Table of Contents
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:
- You can add each character to HashSet.
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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; } } |
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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; } } |
1 2 3 4 5 6 |
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:
- Create a boolean array of 26 length
- Convert char to uppercase and get its ascii value
- Subtract 64 to ascii value to get index between 0 to 25.
- If character is not repeated then we should have false in the boolean array
Java Program to check if String has all unique characters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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; } } |
1 2 3 4 5 6 |
java2blog has all unique chars : false Apple has all unique chars : false index has all unique chars : true world has all unique chars : true |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.