Java Program to Print all Unique Words of a String
Java program to print all unique words present in the string. The task is to print all words occurring only once in the string.
Illustration:
Input : Welcome to Geeks for Geeks.
Output : Welcome
to
for
Input : Java is great.Python is also great.
Output : Java
Python
also
Methods:
This can be done in the following ways:
- Using nested loops
- Using Map
- Using frequency() method of Collections
Naive approach: Using nested loops
The idea to count the occurrence of the string in the string and print if count equals one
- Extract words from string using split() method and store them in an array.
- Iterate over the word array using for loop.
- Use another loop to find the occurrence of the current word the array.
- If the second occurrence is found increment count and set the word to “”
- If the count of the current word is equal to one print it
Example:
Java
public class GFG {
static void printUniqueWords(String str)
{
int count;
String[] words = str.split( "\\W" );
for ( int i = 0 ; i < words.length; i++) {
count = 1 ;
for ( int j = i + 1 ; j < words.length; j++) {
if (words[i].equalsIgnoreCase(words[j])) {
count++;
words[j] = "" ;
}
}
if (count == 1 && words[i] != "" )
System.out.println(words[i]);
}
}
public static void main(String[] args)
{
String str = "Welcome to geeks for geeks" ;
printUniqueWords(str);
}
}
|
Note: Time complexity is of order n^2 where space complexity is of order n
Method 2: Using Map
Approach: The idea is to use Map to keep track of words already occurred. But first, we have to extract all words from a String, as a string may contain many sentences with punctuation marks.
- Create an empty Map.
- Extract words from string using split() method and store them in an array.
- Iterate over the word array.
- Check if the word is already present in the Map or not.
- If a word is present in the map, increment its value by one.
- Else store the word as the key inside the map with value one.
- Iterate over the map and print words whose value is equal to one.
Example:
Java
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class GFG {
static void printUniqueWords(String str)
{
HashMap<String, Integer> map
= new LinkedHashMap<String, Integer>();
String[] words = str.split( "\\W" );
for (String word : words) {
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1 );
}
else
map.put(word, 1 );
}
for (Map.Entry<String, Integer> entry :
map.entrySet()) {
if (entry.getValue() == 1 )
System.out.println(entry.getKey());
}
}
public static void main(String[] args)
{
String str = "Welcome to geeks for geeks" ;
printUniqueWords(str);
}
}
|
Note: Time complexity is of the order of n where space complexity is of order n. Hence, it is the optimal approach.
Method 3 : Using Collections.frequency() . If the occurrence of word in string is 1 , then the word is unique.
Java
import java.util.*;
public class Main{
public static void main(String[] args)
{
String str = "Welcome to geeks for geeks" ;
String[] words = str.split( "\\W" );
List<String> al = new ArrayList<>(Arrays.asList(words));
for (String x:al) {
if (Collections.frequency(al,x)== 1 ){
System.out.println(x);
}
}
}
}
|
Last Updated :
17 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...