Second Most Repeated Word

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].
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
CodingNinjas
author
2y
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 b...read more

Help your peers!
Add answer anonymously...
Visa Fullstack Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter