How do i find a repeated word in a string in python?


In a given sentence there may be a word which get repeated before the sentence ends. In this python program, we are going to catch such word which is repeated in sentence. Below is the logical steps we are going to follow to get this result.

  • Splits the given string into words separated by space.
  • Then we convert these words into Dictionary using collections
  • Traverse this list of words and check which first word has the frequency > 1

Program - Find the Repeated Word

In the below program we use the counter method from the collections package to keep a count of the words.

Example

 Live Demo

from collections import Counter
def Repeat_word(load):
   word = load.split(' ')
   dict = Counter(word)
   for value in word:
      if dict[value]>1:
         print (value)
            return
if __name__ == "__main__":
   input = 'In good time in bad time friends are friends'
   Repeat_word(input)

Running the above code gives us the following result −

Output

time

How do i find a repeated word in a string in python?

Updated on 19-Dec-2019 12:30:11

  • Related Questions & Answers
  • Find the first repeated word in a string in Python?
  • Find the first repeated word in a string in Java
  • Find the first repeated word in a string in C++
  • Find the first repeated character in a string using C++.
  • Find repeated character present first in a string in C++
  • Word Dictionary using Python Tkinter
  • Find the second most repeated word in a sequence in Java
  • Find the first maximum length even word from a string in C++
  • Second most repeated word in a sequence in Python?
  • Longest Word in Dictionary in Python
  • Python - Find the length of the last word in a string
  • Print first letter of each word in a string using C# regex
  • How to capitalize the first letter of each word in a string using JavaScript?
  • Find frequency of each word in a string in Python
  • Getting first letter of each word in a String using regex in Java


One string is given .Our task is to find first repeated word in the given string.To implement this problem we are using Python Collections. From the collection, we can get Counter() method.

Algorithm

Repeatedword(n)
/* n is the string */
Step 1: first split given string separated by space into words.
Step 2: now convert the list of words into a dictionary.
Step 3: traverse list of words and check which the first word has frequency >1

Example code

# To Find the first repeated word in a string  from collections 
import Counter
def repeatedword(n):
   # first split given string separated by space into words
   w = n.split(' ')
   con = Counter(w)
   for key in w:
      if con[key]>1:
         print ("REPEATED WORD IS ::>",key)
         return
# Driver program
if __name__ == "__main__":
   n=input("Enter the String ::")
   repeatedword(n)

Output

Enter the String ::We are all peaceful soul and blissful soul and loveful soul happy soul
REPEATED WORD IS ::> soul

How do i find a repeated word in a string in python?

Updated on 30-Jul-2019 22:30:23

  • Related Questions & Answers
  • Find the first repeated word in a string in Java
  • Find the first repeated word in a string in C++
  • Find the first repeated word in a string in Python using Dictionary
  • Find the first repeated character in a string using C++.
  • Find repeated character present first in a string in C++
  • Find the second most repeated word in a sequence in Java
  • Find the first maximum length even word from a string in C++
  • Second most repeated word in a sequence in Python?
  • Python - Find the length of the last word in a string
  • Find frequency of each word in a string in Python
  • How to replace only the first repeated value in a string in MySQL
  • PHP program to find the first word of a sentence
  • Print first letter of each word in a string in C#
  • Get distinct first word from a string with MongoDB?
  • Python – Word Frequency in a String

Explanation

In this program, we need to find out the duplicate words present in the string and display those words.

To find the duplicate words from the string, we first split the string into words. We count the occurrence of each word in the string. If count is greater than 1, it implies that a word has duplicate in the string.

In above example, the words highlighted in green are duplicate words.

Algorithm

  1. Define a string.
  2. Convert the string into lowercase to make the comparison insensitive.
  3. Split the string into words.
  4. Two loops will be used to find duplicate words. Outer loop will select a word and Initialize variable count to 1. Inner loop will compare the word selected by outer loop with rest of the words.
  5. If a match found, then increment the count by 1 and set the duplicates of word to '0' to avoid counting it again.
  6. After the inner loop, if count of a word is greater than 1 which signifies that the word has duplicates in the string.

Solution

Python

Output:

 Duplicate words in a given string : 
big
black

C

Output:

Duplicate words in a given string : 
big
black

JAVA

Output:

Duplicate words in a given string : 
big
black

C#

Output:

Duplicate words in a given string : 
big
Black

PHP

Output:

Duplicate words in a given string : 
big
black

Next Topic#

How do you find repeated words in Python?

Python.
string = "big black bug bit a big black dog on his big black nose";.
#Converts the string into lowercase..
string = string.lower();.
#Split the string into words using built-in function..
words = string.split(" ");.
print("Duplicate words in a given string : ");.
for i in range(0, len(words)):.
count = 1;.

How do I find a repeated character in a string in Python?

Python.
string = "Great responsibility";.
print("Duplicate characters in a given string: ");.
#Counts each character present in the string..
for i in range(0, len(string)):.
count = 1;.
for j in range(i+1, len(string)):.
if(string[i] == string[j] and string[i] != ' '):.
count = count + 1;.

How do I find the most repeated words in a string?

Program:.
import java. io. BufferedReader;.
import java. io. FileReader;.
import java. util. ArrayList;.
public class MostRepeatedWord {.
public static void main(String[] args) throws Exception {.
String line, word = "";.
int count = 0, maxCount = 0;.
ArrayList<String> words = new ArrayList<String>();.

How do you count the number of times a word appears in a string in Python?

count() One of the built-in ways in which you can use Python to count the number of occurrences in a string is using the built-in string . count() method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method.