5 Ways to Determine if String has all Unique Characters : Java Code with Example

In the technical interviews ,you may come across this question many times ,determine whether a string has all unique characters or not . Before moving to the solution , here in this question   we have taken one assumption that is  all the characters in the string are ASCII characters.
For those who are not familiar with ASCII characters.

ASCII abbreviation is known as American Standard Code for  Information Interchange.
In simple words it is just the number representation of  characters.

So , First understand the question by writing examples :

Input : Alive is awesome 
Output : false

Input : Live present moment
Output : false 

Input : Alive swum
Output: true



PseudoCode for Method 1 :

1. Create a HashSet object.
2. Scan the whole string, and add each character one by one to the HashSet object
3. If the add object  returns true then continue
    else return false 



Method 1

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class uniquechar {
    
    public static void main (String args[])
    {
        boolean result=false;
        String inputstring="Alve i@wsom";
        System.out.println(inputstring);
        HashSet < Character> uniquecharset= new HashSet();
        for(int i=0;i < inputstring.length();i++)
        {
            result=uniquecharset.add(inputstring.charAt(i));
            if (result == false)
            break;
        }
    System.out.println(result); }
}


PseudoCode for Method 2 :

1. Scan the input string , take each character one by one and set count flag to 0.
2. For each character in the inputstring ,Rescan the inputstring and compare the character with each character appear in the inputstring
3. If equal then increase the count by 1
                   else continue the loop
4.  If count flag value is greater than 1 then return false
                  else return true


Method 2

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class UniqueChar2 {
    
    public static void main (String args[])
    {
        boolean result=false;
        String inputstring="Alive is awesome";
        System.out.println("String method 2 answer "+ method2(inputstring));
    }
    
    public static boolean method2(String input)
    {
        for(int i=0; i < input.length();i++)
        {
            char charcterofinputstring=input.charAt(i);
            int count=0;
            for(int j=i; j < input.length();j++)
            {
                if (charcterofinputstring==input.charAt(j))
                count++;
            }
            if(count > 1)
            return false;
        }
        return true;
    }
}
 



PseudoCode for Method 3:

1. indexOf() returns the index of first occurence of the character or else return -1. So , here we are creating an arraylist object.
2. Scan the inputstring and add the index of each character to the arraylist object.
3. Sort the arraylist object.

unique characters in string java example
 
  4. Compare the values of  each adjacent positions of arraylist object
 if equal then return false
                  else continue scanning the arraylist

5.  return true
  




Method 3

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class UniqueChar3 {
    
    public static void main (String args[])
    {
        boolean result=false;
        String inputstring="Alive is awesome";
        System.out.println("String method 3 answer "+ method3(inputstring));
    }
    public static boolean method3(String input)
    {
        ArrayList ar= new ArrayList();
        for (int i=0; i < input.length() ; i++ )
        {
            int j = input.indexOf(input.charAt(i));
            ar.add(j);
        }
        Collections.sort(ar);
        for (int i=0;i < (ar.size()-1);i++)
        {
            if (ar.get(i) == ar.get(i+1))
            return false;
        }
        return true;
    }
}  

 PseudoCode for Method 4 :

1.  For this method we need to know about  two inbuilt functions in java , indexOf() which returns the index of first occurence of the character in the string , while second function lastIndexOf() returns the index of last occurence of the character in the given string.
2. First , we convert the given inputstring into characterarray by using toCharArray() function.
3. Calculate the indexOf() and lastIndexOf() for each character in the given inputstring
4. If both are equal then continue and make result= true
        else set flag result = false
5. Return result


Method 4

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class UniqueChar4 {
    
    public static void main (String args[])
    {
        boolean result=false;
        String inputstring="Alive is awesome";
        System.out.println("String method 4 answer "+ method4(inputstring));
    }
    
    public static boolean method4(String input)
    {
        boolean result=false;
        for (char ch: input.toCharArray())
        {
            if(input.indexOf(ch)== input.lastIndexOf(ch))
            result= true;
            else
            {
                result=false;
                break;
            }
        }
        return result;
    }
}
 
 


Method 5

Most memory efficient answer 

Please mention in the comments what is the time complexity of each of the above method and why ?
Also write in comments if you have any other method to find all the unique characters in string.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry