Print all unique words of a String
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 write.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.
Method 2:using set()
In this approach, we store the string in the form of a set of individual words and print the words. To solve this problem in this method, first we need to know about https://www.geeksforgeeks.org/sets-in-python/
Below is the implementation of the above approach:
Please Login to comment...