Remove duplicates from a given string | Java program to create a unique string ; Function to make the string unique ; loop to traverse the string and check for repeating chars using IndexOf ( ) method in Java ; character at i 'th index of s ; if c is present in str , it returns the index of c , else it returns - 1 ; adding c to str if - 1 is returned ; Driver code ; Input string with repeating chars
import java . util . * ; class IndexOf { public static String unique ( String s ) { String str = new String ( ) ; int len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = s . charAt ( i ) ; if ( str . indexOf ( c ) < 0 ) { str += c ; } } return str ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( unique ( s ) ) ; } }