Given a sequence of strings, the task is to find out the second most repeated (or frequent) string in the given sequence. You may assume that no two words are the second most repeated, there will be always a single word.
Examples:Â
Input: ["aaa", "bbb", "ccc", "bbb", "aaa", "aaa"]
Output: bbb
Explanation: "bbb" is the second most repeated word and is repeated 2 times after "aaa" which is repeated 3 times.
Input: ["geeks", "for", "geeks", "for", "geeks", "aaa"]
Output: for
Explanation: "for" is the second most repeated word and is repeated 2 times after "geeks" which is repeated 3 times.
Table of Content
[Naive Approach] Using Nested Loops – O(n^2*k) Time and O(1) Space
We use nested loops to compare each word with the rest of the words to count its occurrences. Then, we find the second most repeated word by keeping track of the highest and second-highest counts.
#include <bits/stdc++.h>
using namespace std;
string secFrequent(const vector<string>& words) {
int n = words.size();
int fMax = 0, sMax = 0;
string sWord = "";
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (words[i] == words[j]) {
cnt++;
}
}
// Track most and second most repeated words
if (cnt > fMax) {
sMax = fMax;
sWord = words[i];
fMax = cnt;
} else if (cnt > sMax && cnt < fMax) {
sMax = cnt;
sWord = words[i];
}
}
return sWord;
}
int main() {
vector<string> words = {"geeks", "for", "geeks", "for", "geeks", "aaa"};
cout << secFrequent(words) << endl;
return 0;
}
import java.util.*;
public class Main {
public static String secFrequent(List<String> words) {
int n = words.size();
int fMax = 0, sMax = 0;
String sWord = "";
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (words.get(i).equals(words.get(j))) {
cnt++;
}
}
// Track most and second most repeated words
if (cnt > fMax) {
sMax = fMax;
sWord = words.get(i);
fMax = cnt;
} else if (cnt > sMax && cnt < fMax) {
sMax = cnt;
sWord = words.get(i);
}
}
return sWord;
}
public static void main(String[] args) {
List<String> words = Arrays.asList("geeks", "for", "geeks", "for", "geeks", "aaa");
System.out.println(secFrequent(words));
}
}
def secFrequent(words):
n = len(words)
fMax, sMax = 0, 0
sWord = ""
for i in range(n):
cnt = 0
for j in range(n):
if words[i] == words[j]:
cnt += 1
# Track most and second most repeated words
if cnt > fMax:
sMax = fMax
sWord = words[i]
fMax = cnt
elif cnt > sMax and cnt < fMax:
sMax = cnt
sWord = words[i]
return sWord
words = ["geeks", "for", "geeks", "for", "geeks", "aaa"]
print(secFrequent(words))
using System;
using System.Collections.Generic;
class Program {
public static string secFrequent(List<string> words) {
int n = words.Count;
int fMax = 0, sMax = 0;
string sWord = "";
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (words[i] == words[j]) {
cnt++;
}
}
// Track most and second most repeated words
if (cnt > fMax) {
sMax = fMax;
sWord = words[i];
fMax = cnt;
} else if (cnt > sMax && cnt < fMax) {
sMax = cnt;
sWord = words[i];
}
}
return sWord;
}
static void Main() {
List<string> words = new List<string> {"geeks", "for", "geeks", "for", "geeks", "aaa"};
Console.WriteLine(secFrequent(words));
}
}
function secFrequent(words) {
let n = words.length;
let fMax = 0, sMax = 0;
let sWord = "";
for (let i = 0; i < n; i++) {
let cnt = 0;
for (let j = 0; j < n; j++) {
if (words[i] === words[j]) {
cnt++;
}
}
// Track most and second most repeated words
if (cnt > fMax) {
sMax = fMax;
sWord = words[i];
fMax = cnt;
} else if (cnt > sMax && cnt < fMax) {
sMax = cnt;
sWord = words[i];
}
}
return sWord;
}
let words = ["geeks", "for", "geeks", "for", "geeks", "aaa"];
console.log(secFrequent(words));
Output
bbb
 Time Complexity will be O(n2k) where n is the size of array and k is the average word length.
[Expected Approach] Using Hashing – O(n*k) Time and O(n) Space
- We use a map (or dictionary) to store the frequency of each word.
- We iterate over the map and keep track of the highest count and the second highest count.
#include <bits/stdc++.h>
using namespace std;
string secFrequent(const vector<string>& words) {
unordered_map<string, int> wordCount;
for (const string& word : words) {
wordCount[word]++;
}
// Find the most and second most repeated word
int fMax = 0, sMax = 0;
string fWord = "", sWord = "";
for (auto& entry : wordCount) {
if (entry.second > fMax) {
sMax = fMax;
sWord = fWord;
fMax = entry.second;
fWord = entry.first;
} else if (entry.second > sMax && entry.second < fMax) {
sMax = entry.second;
sWord = entry.first;
}
}
return sWord;
}
int main() {
vector<string> words = {"aaa", "bbb", "ccc", "bbb", "aaa", "aaa"};
cout << secFrequent(words) << endl;
return 0;
}
import java.util.*;
public class Main {
public static String secFrequent(List<String> words) {
Map<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
// Find the most and second most repeated word
int fMax = 0, sMax = 0;
String fWord = "", sWord = "";
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
if (entry.getValue() > fMax) {
sMax = fMax;
sWord = fWord;
fMax = entry.getValue();
fWord = entry.getKey();
} else if (entry.getValue() > sMax && entry.getValue() < fMax) {
sMax = entry.getValue();
sWord = entry.getKey();
}
}
return sWord;
}
public static void main(String[] args) {
List<String> words = Arrays.asList("aaa", "bbb", "ccc", "bbb", "aaa", "aaa");
System.out.println(secFrequent(words));
}
}
from collections import Counter
def secFrequent(words):
wordCount = Counter(words)
# Find the most and second most repeated word
fMax, sMax = 0, 0
fWord, sWord = "", ""
for word, count in wordCount.items():
if count > fMax:
sMax = fMax
sWord = fWord
fMax = count
fWord = word
elif count > sMax and count < fMax:
sMax = count
sWord = word
return sWord
words = ["aaa", "bbb", "ccc", "bbb", "aaa", "aaa"]
print(secFrequent(words))
using System;
using System.Collections.Generic;
class Program {
public static string secFrequent(List<string> words) {
var wordCount = new Dictionary<string, int>();
foreach (var word in words) {
if (wordCount.ContainsKey(word))
wordCount[word]++;
else
wordCount[word] = 1;
}
// Find the most and second most repeated word
int fMax = 0, sMax = 0;
string fWord = "", sWord = "";
foreach (var entry in wordCount) {
if (entry.Value > fMax) {
sMax = fMax;
sWord = fWord;
fMax = entry.Value;
fWord = entry.Key;
} else if (entry.Value > sMax && entry.Value < fMax) {
sMax = entry.Value;
sWord = entry.Key;
}
}
return sWord;
}
static void Main() {
List<string> words = new List<string> { "aaa", "bbb", "ccc", "bbb", "aaa", "aaa" };
Console.WriteLine(secFrequent(words));
}
}
function secFrequent(words) {
let wordCount = {};
for (let word of words) {
wordCount[word] = (wordCount[word] || 0) + 1;
}
// Find the most and second most repeated word
let fMax = 0, sMax = 0;
let fWord = "", sWord = "";
for (let word in wordCount) {
if (wordCount[word] > fMax) {
sMax = fMax;
sWord = fWord;
fMax = wordCount[word];
fWord = word;
} else if (wordCount[word] > sMax && wordCount[word] < fMax) {
sMax = wordCount[word];
sWord = word;
}
}
return sWord;
}
let words = ["aaa", "bbb", "ccc", "bbb", "aaa", "aaa"];
console.log(secFrequent(words));
Output
bbb
 Time Complexity will be O(n*k) where n is the size of array and k is the average word length.