Find the repeated word | Wipro previous year question paper solution

Problem:-

'Word Finder' is an online game in which the player needs to enter text. The game auto detects the word occurring more than once in the player's text .At the end of the game the repeated words are display to the player's screen.

Write an algorithm to find the words occurring more than once in the player's text.

input:- the input consists of a string playerText,  representing the text entered by the player.

Output:- Print space separated strings in the lexicographical sorted order representing the repeated words in the player's text . if no word is repeated print "NA".

Note:- A Word is an alphabetic sequence of characters with no whitespaces, and there is no punctuations in the input text.

playerText is a case sensitive (i.e cat and CAT are the different word). It consist the lower case and the upper case letter from the English alphabet.

Example:-

input:-
cat batman latt matter batman mouse mouse latt latt

Output:-
batman latt mouse

Explanation:- The word "batman " is repeated two times , the word "latt" is repeated three times and the word "mouse" is repeated two times in the text . So the repeated words are [batman, latt, mouse].

Code:-
s=input().split()

# empty dictionary
dic={}
# iterate over the list and count the frequency of the elements
for i in s:
# element is not present
# in the dic then add
# it to the dic
if i not in dic:
dic[i]=1
else:
dic[i]+=1
# iterate over the dic and
# if the count of the any element
# greater than 1 then print it
for i in dic:
if dic[i]>1:
print(i,end=" ")

Output:-
cat batman latt matter batman mouse mouse latt latt
batman latt mouse

Recommended Post:

Key points:-

Cracking the coding interview:-

Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

MCQs:-