Find Duplicate Words in String Python

Find Duplicate Words in String Python | This article will show you how to find duplicate words in string Python. We will discuss it with the help of string splitting. Follow the article till the end to understand how to do it.

We will initially break the string into words in terms of finding similar words. After that, we will count how many times each word has appeared in the string. If the count is more than one, it means that word in the string has been repeated. Let us have a look at the following example:-

Find the duplicate words in the string, “Hi this is coder-1 and he is coder2 “. The expected output will be:- The duplicate word in the string is = “is”.

Find Duplicate Words in String Python Code

str = """I know Java, Python, JavaScript and 
       Amelia knows C++, Python, & JavaScript"""
str = str.lower()

# split function
words = str.split(" ")

print("The duplicate word in the string is: ")
# range function
for i in range(0, len(words)):
   count = 1
   for x in range(i+1, len(words)):
      if(words[i] == (words[x])):
         count = count + 1
         # To prevent printing a visited word,
         # set words[x] to 0.
         words[x] = "0"
   # duplicate word if count is more than 1
   if(count > 1 and words[i] != "0"):
      print(words[i])

Output:-

The duplicate word in the string is:
python,
javascript

Looking at the above find duplicate words in string python code in more detail, let’s take another example with the string “Python is popular and Java is also popular” here the expected output is:-

Duplicate strings in the given string are:-
is
popular

The basic logic behind the whole code is pretty simple- we make use of string splitting.

Approach to find duplicate words in string python:

1. Create a string.
2. To avoid case sensitivity, change the string to lowercase.
3. Split the string.
4. To identify duplicate words, two loops will be employed. The word will be chosen in the outer loop, and the variable count will be set to one. The word chosen by the outer loop will be compared against the remaining words in the inner loop.
5. When a match is detected, increase the count by one and make the duplicates of the word ‘0’ to prevent counting it again.
6. If a count of a word is more than one after the inner loop, the word has repetitions or duplicates in the string.

Another Example to Find Duplicate Words in String Python

# python program to find  duplicate words in a string
str = "Python is popular and Java is also popular"
str = str.lower()

# Split function
words = str.split(" ")

print("Duplicate words in the given string is: ")
# range function
for i in range(0, len(words)):
   count = 1
   for x in range(i+1, len(words)):
      if(words[i] == (words[x])):
         count = count + 1
         # To prevent printing a visited word,
         # set words[x] to 0
         words[x] = "0"
   # duplicate word if count is more than 1
   if(count > 1 and words[i] != "0"):
      print(words[i])

Output:-

The duplicate word in the string is:
python,
javascript

This brings us to the end of the article, we hope you learned the appropriate use of the split function to find the duplicate words in the string in Python. Also see:- Find Shortest Word in List Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.