Print all distinct strings from a given array | Java program to implement the above approach ; Function to find the distinct strings from the given array ; Stores distinct strings from the given array ; Traverse the array ; If current string not present into the set ; Insert current string into the set ; Traverse the set DistString ; Print distinct string ; Driver code ; Stores length of the array
import java . io . * ; import java . util . * ; class GFG { static void findDisStr ( List < String > arr , int N ) { Set < String > DistString = new HashSet < String > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! DistString . contains ( arr . get ( i ) ) ) { DistString . add ( arr . get ( i ) ) ; } } for ( String string : DistString ) { System . out . print ( string + " ▁ " ) ; } } public static void main ( String [ ] args ) { List < String > arr = Arrays . asList ( new String [ ] { " Geeks " , " For " , " Geeks " , " Code " , " Coder " } ) ; int N = arr . size ( ) ; findDisStr ( arr , N ) ; } }