Synechron Interview Question
Developer Program EngineersCountry: India
Code for this in python:
word_dict ={ }
with open('test.txt', 'r') as fs:
for line in fs:
word_list = line.split(' ')
for word in word_list:
if word_dict.has_key(word):
word_dict[word] = word_dict[word]+1
else:
word_dict[word] = 1
for k,v in word_dict.items():
print "{} --> {}".format(k,v)
Approach :
- Anonymous September 15, 2017-Split all strings of file by Space
-iterate over String Array & store all the strings in HashMap as key & its value will be incremented by one each time repeating the string.
-compare & store the max count & its value while iterating