Write a function that takes a String as an argument and prints all unique words in it.
Input : Java is great. Grails is also great Output : Java Grails also
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.
For extracting words from a String, refer Extracting each word from a String.
Python: The idea is to use a Dictionary for calculating the count of each word. But first, we have to extract all words from a String because a string may contain punctuation marks. This is done using regex or regular expression. The word which has count 1 in the dictionary is a unique word.
Java Grails also
This article is contributed by Gaurav Ahirwar and Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java program to print Even length words in a String
- Print number of words, vowels and frequency of each character
- Print all possible ways to convert one string into another string | Edit-Distance
- Reverse words in a given String in Java
- Counting number of lines, words, characters and paragraphs in a text file using Java
- Java program to swap first and last characters of words in a sentence
- Swap corner words and reverse middle characters
- Find the count of M character words which have at least one character repeated
- Count of words ending at the given suffix in Java
- Remove duplicate words from Sentence using Regular Expression
- Java Program to Find Duplicate Words in a Regular Expression
- Maximum number of Unique integers in Sub-Array of given size
- Print all possible strings of length k that can be formed from a set of n characters
- Print all nodes between two given levels in Binary Tree
- Java Program to Print all the Strings that Match a Given Pattern from a File
- Java Program to Print All the Repeated Numbers with Frequency in an Array
- Print first letter of each word in a string using regex
- PrintWriter print(String) method in Java with Examples
- PrintStream print(String) method in Java with Examples
- Print the middle character of a string