Write a Java program to find unique words and characters in a text 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<String> uniqueWords = new HashSet<String>(Arrays.asList(words));
         
        for(String s:uniqueWords)
            int i=1;
            System.out.println(i+". "+s);
    }
 
}

Tags:

Java Example