Second Most Repeated Word

Easy
0/40
Average time to solve is 25m
profile
Contributed by
24 upvotes
Asked in companies
AdobeFacebookVisa

Problem statement

You are given an array of strings ‘ARR’. You have to find out the second most repeated word in the array ‘ARR’. It is guaranteed every string occurs a unique number of times in the array. If there is only one unique string in the array, return an empty string.

Example:-
N = 5
S = [‘aaa’, ‘bbb’, ‘ccc’, ‘aaa’, ‘bbb’, ‘aaa’]

ANSWER:- The answer should be ‘bbb’ as it is repeated 2 times and is the second most repeated word in the array [after the word ‘aaa’ which is repeated 3 times].
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains a single integer ‘T’ representing the number of test cases. Then each test case follows.

The first line of every test case contains an integer ‘N’ denoting the length of the array.

The next line of every test case contains ‘N’ strings denoting the string in the array.
Output Format :
For each test case, return the second most repeated character in the array. If there is only one unique string in the array, return an empty string.

The output of each test case should be printed in a separate line.
Note :
You are not required to print anything, it has already been taken care of. Just implement the function.    
Constraints :
1 <= T <= 10
1 <= N <= 1000
1 <= |ARR[i]| <= 1000

Note:- It is guaranteed that every string has a unique frequency in the array.

Time Limit = 1 sec
Sample Input 1 :
2
3
aa bb aa
6
a b b c a a
Sample Output 1 :
bb 
b  
Explanation for Sample Output 1 :
In the first test case, the string ‘bb’ has frequency 1 and is the 2nd most repeated character in the array.
 In the second test case, the string ‘b’ has frequency 2 and is the 2nd most repeated character in the array.
Sample Input 2 :
1
6
a c c c d d
Sample Output 2 :
d  
Hint

Store the frequency of all the strings in the array.

Approaches (1)
Hashing

Iterate through the array and store the frequency of each string in the array. Find out the maximum frequency word which occurs in the array and print the word which has the maximum frequency but has less frequency than the maximum one.

 

Algorithm:-

 

  1. Initialize an empty string named ANS to store the final answer.
  2. Initialize a dictionary named FREQ to store the frequency of the words in the array.
  3. Iterate through the array from 0 to N.(Let’s say the iterator is i).
    1. Increment FREQ[ARR[i]] by 1.
  4. Initialize a variable named MAX_FREQ with the maximum value of the dictionary FREQ.
  5. Initialize a variable named SEC_MAX with 0.
  6. Iterate through the dictionary FREQ.(Let’s say the iterator is i).
    1. If FREQ[i] is greater than SEC_MAX and but less than MAX_FREQ,
      1. Update SEC_MAX to FREQ[i] and ANS to i.
  7. Return ANS.
Time Complexity

O(N*max(|ARR[i]|), where N is the length of the array ARR.

We are iterating through the array once and storing every string in the dictionary which takes |ARR[i]| time each, so the time complexity is O(N*max(|ARR[i]|).

Space Complexity

O(N*max(|ARR[i]|), where N is the length of the array ARR.

We are iterating through the array once and storing every string in the dictionary, so the space complexity is O(N*max(|ARR[i]|).

Code Solution
(100% EXP penalty)
Second Most Repeated Word
Full screen
Console