Skip to content
Related Articles

Related Articles

Find the first repeated word in a string in Python using Dictionary

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 09 Jun, 2022

Prerequisite : Dictionary data structure Given a string, Find the 1st repeated word in a string. Examples:

Input : "Ravi had been saying that he had been there"
Output : had
 
Input : "Ravi had been saying that"
Output : No Repetition

Input : "he had had he"
Output : he

We have existing solution for this problem please refer Find the first repeated word in a string link. We can solve this problem quickly in python using Dictionary data structure. Approach is simple,

  1. First split given string separated by space.
  2. Now convert list of words into dictionary using collections.Counter(iterator) method. Dictionary contains words as key and it’s frequency as value.
  3. Now traverse list of words again and check which first word has frequency greater than 1.

Python3




# Function to Find the first repeated word in a string
from collections import Counter
 
def firstRepeat(input):
     
    # first split given string separated by space
    # into words
    words = input.split(' ')
     
    # now convert list of words into dictionary
    dict = Counter(words)
 
    # traverse list of words and check which first word
    # has frequency > 1
    for key in words:
        if dict[key]>1:
            print (key)
            return
 
# Driver program
if __name__ == "__main__":
    input = 'Ravi had been saying that he had been there'
    firstRepeat(input)

Output:

had

Time Complexity: O(length(words))

Auxiliary Space: O(length(dict))

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!