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

In a given sentence, there may be a word which gets repeated before the sentence ends. In this Python program, we are going to find the first word that appears multiple times in a string using a dictionary approach.

The logical steps we follow to get this result are:

  • Split the given string into words separated by space
  • Convert these words into a dictionary using collections.Counter
  • Traverse the list of words and find the first word with frequency > 1

Using collections.Counter

The Counter class from the collections module creates a dictionary-like object that counts the frequency of each word ?

from collections import Counter

def find_first_repeated_word(text):
    words = text.split(' ')
    word_count = Counter(words)
    
    for word in words:
        if word_count[word] > 1:
            return word
    
    return None

# Test the function
sentence = 'In good time in bad time friends are friends'
result = find_first_repeated_word(sentence)
print("First repeated word:", result)
First repeated word: time

Using Regular Dictionary

We can also solve this using a regular dictionary to count word frequencies ?

def find_first_repeated_word_dict(text):
    words = text.split(' ')
    word_count = {}
    
    # Count word frequencies
    for word in words:
        word_count[word] = word_count.get(word, 0) + 1
    
    # Find first repeated word
    for word in words:
        if word_count[word] > 1:
            return word
    
    return None

# Test the function
sentence = 'Python is great and Python is powerful'
result = find_first_repeated_word_dict(sentence)
print("First repeated word:", result)
First repeated word: Python

Case-Insensitive Version

For case-insensitive matching, convert words to lowercase before processing ?

def find_first_repeated_word_case_insensitive(text):
    words = text.split(' ')
    word_count = {}
    
    # Count frequencies (case-insensitive)
    for word in words:
        lower_word = word.lower()
        word_count[lower_word] = word_count.get(lower_word, 0) + 1
    
    # Find first repeated word
    for word in words:
        if word_count[word.lower()] > 1:
            return word
    
    return None

# Test the function
sentence = 'Hello world hello again'
result = find_first_repeated_word_case_insensitive(sentence)
print("First repeated word:", result)
First repeated word: Hello

Comparison of Methods

Method Advantages Best For
Counter Built-in counting, cleaner code Simple word frequency tasks
Regular Dict More control, no imports needed Custom counting logic
Case-insensitive Handles mixed case words Real-world text processing

Conclusion

Use collections.Counter for simple word counting tasks. For more control over the counting process, use a regular dictionary with get() method. Consider case-insensitive matching for real-world applications.

Updated on: 2026-03-15T17:12:28+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements