Python | Remove all duplicate words from a given sentence

Counters | Python Methods and Functions

Examples:

 Input: Geeks for Geeks Output: Geeks for Input: Python is great and Java is also great Output: is also Java Python and great 

We can solve this problem quickly by using the

from collections import Counter

 

def remov_duplicates ( input ):

  

  # split the input string with a space

input = input . split ( " " )

  

  # joins two adjacent elements in an iterative fashion

for i in range ( 0 , len ( input )):

input [i] = "". join ( input [i])

 

# now create a dictionary using the counter method

# which will ut lines as a key and their

# frequencies as value

UniqW = Counter ( input )

 

# joins two adjacent elements in an iterative manner

s = "" . join (UniqW.keys ())

print (s)

 
# Driver program

if __ name__ = = "__ main__" :

input = 'Python is great and Java is also great'

  remov_duplicates ( input )

Output:

 is also Java Python and great 




Tutorials