Given a string, determine if the string has all unique characters.
Examples :  
Input : abcd10jk
Output : true
Input : hutg9mnd!nk9
Output : false
Approach 1 – Brute Force technique: Run 2 loops with variable i and j. Compare str[i] and str[j]. If they become equal at any point, return false. 
C++
| 
#include <bits/stdc++.h>
 usingnamespacestd;
   booluniqueCharacters(string str)
 {
       
     
     for(inti = 0; i < str.length() - 1; i++) {
         for(intj = i + 1; j < str.length(); j++) {
             if(str[i] == str[j]) {
                 returnfalse;
             }
         }
     }
       
     
     returntrue;
 }
   intmain()
 {
     string str = "neveropen";
       if(uniqueCharacters(str)) {
         cout << "The String "<< str
              << " has all unique characters\n";
     }
     else{
         cout << "The String "<< str
              << " has duplicate characters\n";
     }
     return0;
 }
 | 
 
 
 
 
Java
| 
importjava.util.*;
   classGfG {
     booleanuniqueCharacters(String str)
     {
         
         
         for(inti = 0; i < str.length(); i++)
             for(intj = i + 1; j < str.length(); j++)
                 if(str.charAt(i) == str.charAt(j))
                     returnfalse;
           
         
         returntrue;
     }
       publicstaticvoidmain(String args[])
     {
         GfG obj = newGfG();
         String input = "neveropen";
           if(obj.uniqueCharacters(input))
             System.out.println("The String "+ input + " has all unique characters");
         else
             System.out.println("The String "+ input + " has duplicate characters");
     }
 }
 | 
 
 
 
 
Python3
| 
  defuniqueCharacters(str):
     
     
     
     fori inrange(len(str)):
         forj inrange(i +1,len(str)): 
             if(str[i] ==str[j]):
                 returnFalse;
       
     
     returnTrue;
     str="neveropen";
   if(uniqueCharacters(str)):
     print("The String ", str," has all unique characters");
 else:
     print("The String ", str, " has duplicate characters");
   | 
 
 
 
 
C#
| 
usingSystem;
   publicclassGFG {
       staticbooluniqueCharacters(String str)
     {
           
         
         for(inti = 0; i < str.Length; i++)
             for(intj = i + 1; j < str.Length; j++)
                 if(str[i] == str[j])
                     returnfalse;
           
         
         returntrue;
     }
       publicstaticvoidMain()
     {
         stringinput = "neveropen";
           if(uniqueCharacters(input) == true)
             Console.WriteLine("The String "+ input
                               + " has all unique characters");
         else
             Console.WriteLine("The String "+ input
                               + " has duplicate characters");
     }
 }
   | 
 
 
 
 
PHP
| 
<?php
   functionuniqueCharacters($str) 
 {
     
     
     
     for($i= 0; $i< strlen($str); $i++)
     {
         for($j= $i+ 1; $j< strlen($str); $j++) 
         {
             if($str[$i] == $str[$j])
             {
                 returnfalse;
             }
         }
     }
     
     
     
     returntrue;
 }
   $str= "neveropen";
   if(uniqueCharacters($str)) 
 {
     echo"The String ", $str,
           " has all unique characters\n";
 }
 else
 {
     echo"The String ", $str, 
          " has duplicate characters\n";
 }
   ?>
 | 
 
 
 
 
Javascript
| 
<script>
   functionuniqueCharacters(str)
 {
     
     
     
     for(let i = 0; i < str.length; i++)
         for(let j = i + 1; j < str.length; j++)
             if(str[i] == str[j])
                 returnfalse;
       
     
     returntrue;
 }
   let input = "neveropen";
   if(uniqueCharacters(input) == true)
     document.write("The String "+ input + 
                    " has all unique characters"+ "</br>");
 else
     document.write("The String "+ input + 
                    " has duplicate characters");
                  
   </script>
 | 
 
 
 
 
 
Output : 
The String neveropen has duplicate characters
Time Complexity: O(n2) 
Auxiliary Space: O(1)
Note: Please note that the program is case-sensitive.
Approach 2 – Sorting: Using sorting based on ASCII values of characters 
C++
| 
#include <bits/stdc++.h>
 usingnamespacestd;
   booluniqueCharacters(string str)
 {
       
     sort(str.begin(), str.end());
       for(inti = 0; i < str.length()-1; i++) {
           
         
         
         if(str[i] == str[i + 1]) {
             returnfalse;
         }
     }
     returntrue;
 }
   intmain()
 {
       string str = "neveropen";
       if(uniqueCharacters(str)) {
         cout << "The String "<< str
              << " has all unique characters\n";
     }
     else{
           cout << "The String "<< str
              << " has duplicate characters\n";
     }
     return0;
 }
 | 
 
 
 
 
Java
| 
importjava.util.*;
   classGfG {
     
        
     booleanuniqueCharacters(String str)
     {
         char[] chArray = str.toCharArray();
           
         
         
         Arrays.sort(chArray);
           for(inti = 0; i < chArray.length - 1; i++) {
             
             
             if(chArray[i] != chArray[i + 1])
                 continue;
               
             
             else
                 returnfalse;
         }
         returntrue;
     }
       
     publicstaticvoidmain(String args[])
     {
         GfG obj = newGfG();
         String input = "neveropen";
           if(obj.uniqueCharacters(input))
             System.out.println("The String "+ input
                                + " has all unique characters");
         else
             System.out.println("The String "+ input
                                + " has duplicate characters");
     }
 }
 | 
 
 
 
 
Python3
| 
defuniqueCharacters(st):
       
     st =sorted(st)
       fori inrange(len(st)-1):
           
         
         
         if(st[i] ==st[i +1]) :
             returnFalse
             
     returnTrue
   if__name__=='__main__': 
       st ="neveropen"
       if(uniqueCharacters(st)) :
         print("The String",st,"has all unique characters\n")
     
     else:
         print("The String",st,"has duplicate characters\n")
     
   | 
 
 
 
 
C#
| 
usingSystem;
   publicclassGFG {
       
     
     staticbooluniqueCharacters(String str)
     {
         char[] chArray = str.ToCharArray();
           
         Array.Sort(chArray);
           for(inti = 0; i < chArray.Length - 1; i++) {
               
             
             if(chArray[i] != chArray[i + 1])
                 continue;
               
             
             else
                 returnfalse;
         }
           returntrue;
     }
       
     publicstaticvoidMain()
     {
         stringinput = "neveropen";
           if(uniqueCharacters(input) == true)
             Console.WriteLine("The String "+ input
                               + " has all unique characters");
         else
             Console.WriteLine("The String "+ input
                               + " has duplicate characters");
     }
 }
   | 
 
 
 
 
Javascript
| 
<script>
       
     
     
     
     
     
     functionuniqueCharacters(str)
     {
         let chArray = str.split('');
  
         
         chArray.sort();
  
         for(let i = 0; i < chArray.length - 1; i++) 
         {
  
             
             
             if(chArray[i] != chArray[i + 1])
                 continue;
  
             
             
             else
                 returnfalse;
         }
  
         returntrue;
     }
     
     let input = "neveropen";
  
     if(uniqueCharacters(input) == true)
       document.write("The String "+ input + 
       " has all unique characters"+ "</br>");
     else
       document.write("The String "+ input + 
       " has duplicate characters"+ "</br>");
     
 </script>
 | 
 
 
 
 
 
Output: 
The String neveropen has duplicate characters
Time Complexity: O(nlogn)  
Auxiliary Space: O(1)
Approach 3 – Use of Extra Data Structure: This approach assumes ASCII char set(8 bits). The idea is to maintain a boolean array for the characters. The 256 indices represent 256 characters. All the array elements are initially set to false. As we iterate over the string, set true at the index equal to the int value of the character. If at any time, we encounter that the array value is already true, it means the character with that int value is repeated. 
C++
| 
#include <cstring>
 #include <iostream>
 usingnamespacestd;
   constintMAX_CHAR = 256;
   booluniqueCharacters(string str)
 {
       
     
     if(str.length() > MAX_CHAR)
         returnfalse;
       boolchars[MAX_CHAR] = { 0 };
     for(inti = 0; i < str.length(); i++) {
         if(chars[int(str[i])] == true)
             returnfalse;
           chars[int(str[i])] = true;
     }
     returntrue;
 }
   intmain()
 {
     string str = "neveropen";
       if(uniqueCharacters(str)) {
         cout << "The String "<< str
              << " has all unique characters\n";
     }
     else{
           cout << "The String "<< str
              << " has duplicate characters\n";
     }
     return0;
 }
 | 
 
 
 
 
Java
| 
importjava.util.*;
   classGfG {
     intMAX_CHAR = 256; 
       booleanuniqueCharacters(String str)
     {
         
         
         if(str.length() > MAX_CHAR)
             returnfalse;
           boolean[] chars = newboolean[MAX_CHAR];
         Arrays.fill(chars, false);
           for(inti = 0; i < str.length(); i++) {
             intindex = (int)str.charAt(i);
               
                
             if(chars[index] == true)
                 returnfalse;
               chars[index] = true;
         }
           
         returntrue;
     }
       
     publicstaticvoidmain(String args[])
     {
         GfG obj = newGfG();
         String input = "neveropen";
           if(obj.uniqueCharacters(input))
             System.out.println("The String "+ input
                                + " has all unique characters");
         else
             System.out.println("The String "+ input
                                + " has duplicate characters");
     }
 }
 | 
 
 
 
 
Python3
| 
MAX_CHAR =256;
   defuniqueCharacters(string):
     n =len(string)
     
     
     
     
     ifn > MAX_CHAR:
         returnFalse
       chars =[False] *MAX_CHAR
       fori inrange(n):
         index =ord(string[i])
           
          
          
          
         if(chars[index] ==True):
             returnFalse
           chars[index] =True
       
         
     returnTrue
   if__name__ =='__main__':
   
     input="neveropen"
     if(uniqueCharacters(input)):
         print("The String", input, 
               "has all unique characters")
     else:
         print("The String", input, 
               "has duplicate characters")
   | 
 
 
 
 
C#
| 
usingSystem;
   classGfG {
     staticintMAX_CHAR = 256;
       booluniqueCharacters(String str)
     {
         
         
         if(str.Length > MAX_CHAR)
             returnfalse;
           bool[] chars = newbool[MAX_CHAR];
         for(inti = 0; i < MAX_CHAR; i++) {
             chars[i] = false;
         }
         for(inti = 0; i < str.Length; i++) {
             intindex = (int)str[i];
               
             
             if(chars[index] == true)
                 returnfalse;
               chars[index] = true;
         }
           
         returntrue;
     }
       
     publicstaticvoidMain(String[] args)
     {
         GfG obj = newGfG();
         String input = "neveropen";
           if(obj.uniqueCharacters(input))
             Console.WriteLine("The String "+ input
                               + " has all unique characters");
         else
             Console.WriteLine("The String "+ input
                               + " has duplicate characters");
     }
 }
   | 
 
 
 
 
Javascript
| 
<script>
     
     
     
     let MAX_CHAR = 256;
  
     functionuniqueCharacters(str)
     {
         
         
         if(str.length > MAX_CHAR)
             returnfalse;
  
         let chars = newArray(MAX_CHAR);
         for(let i = 0; i < MAX_CHAR; i++) {
             chars[i] = false;
         }
         for(let i = 0; i < str.length; i++) {
             let index = str[i].charCodeAt();
  
             
             
             if(chars[index] == true)
                 returnfalse;
  
             chars[index] = true;
         }
  
         
         returntrue;
     }
     
     let input = "neveropen";
       if(uniqueCharacters(input))
       document.write("The String "+ input
                         + " has all unique characters");
     else
       document.write("The String "+ input
                         + " has duplicate characters");
     
 </script>
 | 
 
 
 
 
 
Output:
The String neveropen has duplicate characters
Time Complexity: O(n) 
Auxiliary Space: O(n)
Approach 4 – Without Extra Data Structure: The approach is valid for strings having alphabet as a-z. This approach is a little tricky. Instead of maintaining a boolean array, we maintain an integer value called checker(32 bits). As we iterate over the string, we find the int value of the character with respect to ‘a’ with the statement int bitAtIndex = str.charAt(i)-‘a’; 
Then the bit at that int value is set to 1 with the statement 1 << bitAtIndex . 
Now, if this bit is already set in the checker, the bit AND operation would make the checker > 0. Return false in this case. 
Else Update checker to make the bit 1 at that index with the statement checker = checker | (1 <<bitAtIndex); 
C++
| 
#include <bits/stdc++.h>
 usingnamespacestd;
   booluniqueCharacters(string str)
 {
       
     
     intchecker = 0;
       for(inti = 0; i < str.length(); i++) {
           intbitAtIndex = str[i] - 'a';
           
         
         if((checker & (1 << bitAtIndex)) > 0) {
             returnfalse;
         }
           
         
         checker = checker | (1 << bitAtIndex);
     }
       
     returntrue;
 }
   intmain()
 {
       string str = "neveropen";
       if(uniqueCharacters(str)) {
         cout << "The String "<< str
              << " has all unique characters\n";
     }
     else{
         cout << "The String "<< str
              << " has duplicate characters\n";
     }
     return0;
 }
 | 
 
 
 
 
Java
| 
importjava.util.*;
   classGfG {
     booleanuniqueCharacters(String str)
     {
         
         
         intchecker = 0;
           for(inti = 0; i < str.length(); i++) {
             intbitAtIndex = str.charAt(i) - 'a';
               
             
             if((checker & (1<< bitAtIndex)) > 0)
                 returnfalse;
               
             
             checker = checker | (1<< bitAtIndex);
         }
           
         returntrue;
     }
       
     publicstaticvoidmain(String args[])
     {
         GfG obj = newGfG();
         String input = "geekforneveropen";
           if(obj.uniqueCharacters(input))
             System.out.println("The String "+ input
                                + " has all unique characters");
         else
             System.out.println("The String "+ input
                                + " has duplicate characters");
     }
 }
 | 
 
 
 
 
Python3
| 
importmath
   defuniqueCharacters(string):
     
     
     
     checker =0
     
     fori inrange(len(string)):
         bitAtIndex =ord(string[i]) -ord('a')
           
         
         if((bitAtIndex) > 0):
             if((checker & ((1<< bitAtIndex))) > 0):
                 returnFalse
                 
             
             
             checker =checker | (1<< bitAtIndex)
       
     returnTrue
   if__name__ =='__main__':
     
     input="geekforneveropen"
       if(uniqueCharacters(input)):
         print("The String "+input+
               " has all unique characters")
     else:
         print("The String "+input+
               " has duplicate characters")
   | 
 
 
 
 
C#
| 
usingSystem;
   classGFG {
     publicvirtualbooluniqueCharacters(stringstr)
     {
         
         
         
         intchecker = 0;
           for(inti = 0; i < str.Length; i++) {
             intbitAtIndex = str[i] - 'a';
               
             
             if((checker & (1 << bitAtIndex)) > 0) {
                 returnfalse;
             }
               
             
             checker = checker | (1 << bitAtIndex);
         }
           
         
         returntrue;
     }
       
     publicstaticvoidMain(string[] args)
     {
         GFG obj = newGFG();
         stringinput = "geekforneveropen";
           if(obj.uniqueCharacters(input)) {
             Console.WriteLine("The String "+ input + " has all unique characters");
         }
         else{
             Console.WriteLine("The String "+ input + " has duplicate characters");
         }
     }
 }
   | 
 
 
 
 
PHP
| 
<?php
 functionuniqueCharacters($str) 
 {
     
     
     
     
     $checker= 0;
     
     for($i= 0; $i< strlen($str); $i++) 
     {
         $bitAtIndex= $str[$i] - 'a';
         
         
         
         if(($checker& 
             (1 << $bitAtIndex)) > 0) 
         {
             returnfalse;
         }
         
     
     
     $checker= $checker| 
                (1 << $bitAtIndex);
     }
     
     
     
     returntrue;
 }
   $str= "neveropen";
   if(uniqueCharacters($str)) 
 {
     echo"The String ", $str,
          " has all unique characters\n";
 }
 else
 {
     echo"The String ", $str, 
          " has duplicate characters\n";
 }
   ?>
 | 
 
 
 
 
Javascript
| 
<script>
     
     
     
     
     functionuniqueCharacters(str)
     {
         
         
         
         let checker = 0;
  
         for(let i = 0; i < str.length; i++) {
             let bitAtIndex = str[i].charCodeAt(0) - 'a'.charCodeAt(0);
  
             
             
             if((checker & (1 << bitAtIndex)) > 0) {
                 returnfalse;
             }
  
             
             
             checker = checker | (1 << bitAtIndex);
         }
  
         
         
         returntrue;
     }
     
     let input = "geekforneveropen";
  
     if(uniqueCharacters(input)) {
       document.write("The String "+ input + " has all unique characters");
     }
     else{
       document.write("The String "+ input + " has duplicate characters");
     }
     
 </script>
 | 
 
 
 
 
 
Output : 
The String GeekforGeeks has duplicate characters
Time Complexity: O(n)  
Auxiliary Space: O(1)
Exercise: Above program is case insensitive, you can try making the same program that is case sensitive i.e Geeks and GEeks both give different output.
Using Java Stream : 
C++
| 
#include <bits/stdc++.h>
 usingnamespacestd;
   booluniqueCharacters(string s)
 {
     
   
   for(inti = 0; i < s.size(); i++) {
     for(intj = i + 1; j < s.size(); j++) {
       if(s[i] == s[j]) {
         returnfalse;
       }
     }
   }
   returntrue;
 }
   intmain()
 {
   string input = "neveropen";
   if(uniqueCharacters(input))
     cout << "The String "<< input
     << " has all unique characters\n";
   else
     cout << "The String "<< input
     << " has duplicate characters\n";
   return0;
 }
   | 
 
 
 
 
Java
| 
importjava.util.Collections;
 importjava.util.stream.Collectors;
 classGfG {
     booleanuniqueCharacters(String s)
     {
         
         
         returns.chars().filter(e-> Collections.frequency(s.chars().boxed().collect(Collectors.toList()), e) > 1).count() > 1? false: true;
     }
  
     publicstaticvoidmain(String args[])
     {
         GfG obj = newGfG();
         String input = "neveropen";
  
         if(obj.uniqueCharacters(input))
             System.out.println("The String "+ input + " has all unique characters");
         else
             System.out.println("The String "+ input + " has duplicate characters");
     }
 }
   | 
 
 
 
 
Python3
| 
fromcollections importCounter
   defuniqueCharacters(s):
   
     
     
     returnnotany(filter(lambdax: x > 1, list(Counter(list(s)).values())))
   input="neveropen"
   ifuniqueCharacters(input):
     print("The String "+input+" has all unique characters")
         
 else:
     print("The String "+input+" has duplicate characters")
        
 | 
 
 
 
 
C#
| 
usingSystem.Linq;
   classGfG {
   publicboolUniqueCharacters(strings) {
       
     
     returns.ToCharArray().Count(e => s.Count(f => f == e) > 1) > 1 ? false: true;
   }
     
   staticvoidMain(string[] args) {
     GfG obj = newGfG();
     stringinput = "neveropen";
       if(obj.UniqueCharacters(input))
       System.Console.WriteLine("The String "+ input + " has all unique characters");
     else
       System.Console.WriteLine("The String "+ input + " has duplicate characters");
   }
 }
   | 
 
 
 
 
Javascript
| 
  functionuniqueCharacters(s)
 {
     
     
     let arr = s.split("");
     return!arr.some((v, i) => arr.indexOf(v) < i);
 }
   let input = "neveropen";
   if(uniqueCharacters(input))
     console.log("The String "+ input + " has all unique characters");
 else
     console.log("The String "+ input + " has duplicate characters");
         | 
 
 
 
 
 
Reference: 
Cracking the Coding Interview by Gayle 
Approach 5: Using sets() function:
- Convert the string to set.
- If the length of set is equal to the length of the string then return True else False.
Below is the implementation of the above approach
C++
| 
#include <bits/stdc++.h>
 usingnamespacestd;
   booluniqueCharacters(string str)
 {
     set<char> char_set;
       
     for(charc : str)
     {
         char_set.insert(c);
     }
       
     
     returnchar_set.size() == str.size();
 }
   intmain()
 {
     string str = "neveropen";
       if(uniqueCharacters(str))
     {
         cout << "The String "<< str
              << " has all unique characters\n";
     }
     else
     {
         cout << "The String "<< str
              << " has duplicate characters\n";
     }
     return0;
 }
   | 
 
 
 
 
Java
| 
importjava.util.*;
   classGFG{
   staticbooleanuniqueCharacters(String str)
 {
     HashSet<Character> char_set = newHashSet<>();
       
     for(intc  = 0; c< str.length();c++)
     {
         char_set.add(str.charAt(c));
     }
       
     
     returnchar_set.size() == str.length();
 }
   publicstaticvoidmain(String[] args)
 {
     String str = "neveropen";
       if(uniqueCharacters(str))
     {
         System.out.print("The String "+  str
             + " has all unique characters\n");
     }
     else
     {
         System.out.print("The String "+  str
             + " has duplicate characters\n");
     }
 }
 }
     | 
 
 
 
 
Python3
| 
defuniqueCharacters(str):
   
     
     setstring =set(str)
     
     
     
     if(len(setstring) ==len(str)):
         returnTrue
       
     returnFalse
     if__name__ =='__main__':
       input="neveropen"
       if(uniqueCharacters(input)):
         print("The String "+input+
               " has all unique characters")
     else:
         print("The String "+input+
               " has duplicate characters")
   | 
 
 
 
 
C#
| 
usingSystem;
 usingSystem.Collections.Generic;
   publicclassGFG {
       staticbooluniquechars(String str)
     {
         HashSet<char> char_set = newHashSet<char>();
           
         for(intc = 0; c < str.Length; c++) {
             char_set.Add(str);
         }
           
         
         if(char_set.Count == str.Length) {
             returntrue;
         }
         else{
             returnfalse;
         }
     }
       
     publicstaticvoidMain(String[] args)
     {
         String str = "neveropen";
           if(uniquechars(str)) {
             Console.Write("The String "+ str
                           + " has all unique characters\n");
         }
         else{
             Console.Write("The String "+ str
                           + " has duplicate characters\n");
         }
     }
 }
   | 
 
 
 
 
Javascript
| 
<script>
   functionuniqueCharacters(str)
 {
     
     
     varsetstring = newSet(str)
     
     
     
     if(setstring.size == str.length)
     {
         returntrue
     }
     else
     { 
         returnfalse
     }
 }
   varinput = "neveropen"
   if(uniqueCharacters(input))
 {
     document.write("The String "+ input +
                    " has all unique characters") ;
 }
 else
 {
     document.write("The String "+ input +
                    " has duplicate characters")
 }
     </script>
 | 
 
 
 
 
 
Output:
The String neveropen has duplicate characters
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
This article is contributed by Saloni Baweja. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
                                            Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
                                            Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!