Skip to content
Related Articles

Related Articles

Python | Remove all duplicates words from a given sentence

Improve Article
Save Article
  • Last Updated : 12 Sep, 2022
Improve Article
Save Article

Given a sentence containing n words/strings. Remove all duplicates words/strings which are similar to each others.

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 using python Counter() method. Approach is very simple.

1) Split input sentence separated by space into words. 
2) So to get all those strings together first we will join each string in given list of strings. 
3) Now create a dictionary using Counter method having strings as keys and their frequencies as values. 
4) Join each words are unique to form single string. 

Python




from collections import Counter
 
def remov_duplicates(input):
 
    # split input string separated by space
    input = input.split(" ")
 
    # now create dictionary using counter method
    # which will have strings as key and their
    # frequencies as value
    UniqW = Counter(input)
 
    # joins two adjacent elements in iterable way
    s = " ".join(UniqW.keys())
    print (s)
 
# Driver program
if __name__ == "__main__":
    input = 'Python is great and Java is also great'
    remov_duplicates(input)


Output

and great Java Python is also

Method 2:  

Python




# Program without using any external library
s = "Python is great and Java is also great"
l = s.split()
k = []
for i in l:
 
    # If condition is used to store unique string
    # in another list 'k'
    if (s.count(i)>=1 and (i not in k)):
        k.append(i)
print(' '.join(k))


Output

Python is great and Java also

Method 3: Another shorter implementation:

Python3




# Python3 program
 
string = 'Python is great and Java is also great'
 
print(' '.join(dict.fromkeys(string.split())))


Output

Python is great and Java also

Method: Using set() 

Python3




string = 'Python is great and Java is also great'
print(' '.join(set(string.split())))


Output

is Python great and also Java

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!