Determine if a string has all Unique Characters | Java program to illustrate String with unique characters using set data structure ; Inserting character of String into set ; If length of set is equal to len of String then it will have unique characters ; Driver code
import java . util . * ; class GFG { static boolean uniqueCharacters ( String str ) { HashSet < Character > char_set = new HashSet < > ( ) ; for ( int c = 0 ; c < str . length ( ) ; c ++ ) { char_set . add ( str . charAt ( c ) ) ; } if ( char_set . size ( ) == str . length ( ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; if ( uniqueCharacters ( str ) ) { System . out . print ( " The ▁ String ▁ " + str + " has all unique charactersNEW_LINE"); } else { System . out . print ( " The ▁ String ▁ " + str + " has duplicate charactersNEW_LINE"); } } }