Python Program To Remove all Duplicates Words from a Sentence

Counter() function in Python:

Python Counter is a container that keeps track of the count of each element in the container. The counter class is a subclass of the dictionary class.

The counter class is a subclass of the dictionary class. You can count the key-value pairs in an object, also known as a hash table object, using the Python Counter tool.

split() Method:

To extract the words from the string, we used the split() method with space as the separator, which returns a list of the words in the string.

key() method :

The key() method will be used to retrieve the keys of a dictionary. It returns all of the keys in a dictionary.

Given a string/sentence and the task is to remove all the duplicate words from a given sentence.

Examples:

Example1:

Input:

Given String =  'good morning btechgeeks good morning hello all all'

Output:

The given sentence after the removal of all duplicate words :
good morning btechgeeks hello all

Example2:

Input:

Given String = ' health is is wealth so protect it so so'

Output:

The given sentence after the removal of all duplicate words :
health is wealth so protect it

Program To Remove all Duplicates Words from a Sentence

Below are the ways to remove all the duplicate words from a given sentence.

Method #1: Using Counter() Function (Static Input)

Approach:

  • Import the Counter() function from the collections module using the import keyword.
  • Give the string as static input and store it in a variable.
  • Split the given string into a list of words using the split() function and store it in another variable.
  • Apply Counter() function on the above-obtained list of words and store it in another variable.
  • Join all the keys of the above-obtained dictionary using the join() function and store it in another variable.
  • Print the given sentence after the removal of all duplicate words.
  • The Exit of the program.

Below is the implementation:

# Import the Counter() function from the collections module using the import keyword.
from collections import Counter
# Give the string as static input and store it in a variable.
gvn_str = 'good morning btechgeeks good morning hello all all'
# Split the given string into a list of words using the split() function and
# store it in another variable.
splt_str = gvn_str.split(" ")
# Apply Counter() function on the above-obtained list of words and store it
# in another variable.
dictinry = Counter(splt_str)
# Join all the keys of the above-obtained dictionary using the join() function
# and store it in another variable.
reslt = " ".join(dictinry.keys())
# Print the given sentence after the removal of all duplicate words.
print("The given sentence after the removal of all duplicate words :")
print(reslt)

Output:

The given sentence after the removal of all duplicate words :
good morning btechgeeks hello all

Method #2: Using Counter() Function (User Input)

Approach:

  • Import the Counter() function from the collections module using the import keyword.
  • Give the string as user input using the input() function and store it in a variable.
  • Split the given string into a list of words using the split() function and store it in another variable.
  • Apply Counter() function on the above-obtained list of words and store it in another variable.
  • Join all the keys of the above-obtained dictionary using the join() function and store it in another variable.
  • Print the given sentence after the removal of all duplicate words.
  • The Exit of the program.

Below is the implementation:

# Import the Counter() function from the collections module using the import keyword.
from collections import Counter
# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string : ")
# Split the given string into a list of words using the split() function and
# store it in another variable.
splt_str = gvn_str.split(" ")
# Apply Counter() function on the above-obtained list of words and store it
# in another variable.
dictinry = Counter(splt_str)
# Join all the keys of the above-obtained dictionary using the join() function
# and store it in another variable.
reslt = " ".join(dictinry.keys())
# Print the given sentence after the removal of all duplicate words.
print("The given sentence after the removal of all duplicate words :")
print(reslt)

Output:

Enter some random string : health is is wealth so protect it so so
The given sentence after the removal of all duplicate words :
health is wealth so protect it