Remove duplicates from string keeping the order according to last occurrences | Java program to remove duplicate character from character array and print in sorted order ; Used as index in the modified String ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
import java . util . * ; class GFG { static String removeDuplicates ( String str ) { int n = str . length ( ) ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = i + 1 ; j < n ; j ++ ) if ( str . charAt ( i ) == str . charAt ( j ) ) break ; if ( j == n ) res = res + str . charAt ( i ) ; } return res ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( removeDuplicates ( str ) ) ; } }