java program to print unique words in a sentence code example

Example: java program to print unique words in a sentence

import java.util.Arrays;
import java.util.HashSet;
 
/**
 * Java Program - Find Unique Words
 */
public class Example {
 
    public static void main(String[] args) {
 
        String str = "apple banana mango grape lichi mango apple grape";
         
        String[] words = str.split(" ");
         
        HashSet uniqueWords = new HashSet(Arrays.asList(words));
         
        for(String s:uniqueWords)
            int i=1;
            System.out.println(i+". "+s);
    }
 
}

Tags:

Misc Example