Find the first repeated word in a string in Python using Dictionary
Prerequisite : Dictionary data structure
Given a string, Find the 1st repeated word in a string.
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
- First split given string separated by space.
- Now convert list of words into dictionary using collections.Counter(iterator) method. Dictionary contains words as key and it’s frequency as value.
- Now traverse list of words again and check which first word has frequency greater than 1.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course