Given a sequence of strings, the task is to find out the second most repeated (or frequent) string in the given sequence.(Considering 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
Input : {"geeks", "for", "geeks", "for",
"geeks", "aaa"}
Output : for
Asked in : Amazon
- Store all the words in a map with their occurrence with word as key and its occurrence as value.
- Find the second largest value in the map.
- Traverse the map again and return the word with occurrence value equals to second max value.
C++
// C++ program to find out the second // most repeated word #include <bits/stdc++.h> using namespace std; // Function to find the word string secMostRepeated(vector<string> seq) { // Store all the words with its occurrence unordered_map<string, int> occ; for (int i = 0; i < seq.size(); i++) occ[seq[i]]++; // find the second largest occurrence int first_max = INT_MIN, sec_max = INT_MIN; for (auto it = occ.begin(); it != occ.end(); it++) { if (it->second > first_max) { sec_max = first_max; first_max = it->second; } else if (it->second > sec_max && it->second != first_max) sec_max = it->second; } // Return string with occurrence equals // to sec_max for (auto it = occ.begin(); it != occ.end(); it++) if (it->second == sec_max) return it->first; } // Driver program int main() { vector<string> seq = { "ccc", "aaa", "ccc", "ddd", "aaa", "aaa" }; cout << secMostRepeated(seq); return 0; } |
Java
// Java program to find out the second // most repeated word import java.util.*; class GFG { // Method to find the word static String secMostRepeated(Vector<String> seq) { // Store all the words with its occurrence HashMap<String, Integer> occ = new HashMap<String,Integer>(seq.size()){ @Override public Integer get(Object key) { return containsKey(key) ? super.get(key) : 0; } }; for (int i = 0; i < seq.size(); i++) occ.put(seq.get(i), occ.get(seq.get(i))+1); // find the second largest occurrence int first_max = Integer.MIN_VALUE, sec_max = Integer.MIN_VALUE; Iterator<Map.Entry<String, Integer>> itr = occ.entrySet().iterator(); while (itr.hasNext()) { Map.Entry<String, Integer> entry = itr.next(); int v = entry.getValue(); if( v > first_max) { sec_max = first_max; first_max = v; } else if (v > sec_max && v != first_max) sec_max = v; } // Return string with occurrence equals // to sec_max itr = occ.entrySet().iterator(); while (itr.hasNext()) { Map.Entry<String, Integer> entry = itr.next(); int v = entry.getValue(); if (v == sec_max) return entry.getKey(); } return null; } // Driver method public static void main(String[] args) { String arr[] = { "ccc", "aaa", "ccc", "ddd", "aaa", "aaa" }; List<String> seq = Arrays.asList(arr); System.out.println(secMostRepeated(new Vector<>(seq))); } } // This program is contributed by Gaurav Miglani |
C#
// C# program to find out the second // most repeated word using System; using System.Collections.Generic; class GFG { // Method to find the word static String secMostRepeated(List<String> seq) { // Store all the words with its occurrence Dictionary<String, int> occ = new Dictionary<String, int>(); for (int i = 0; i < seq.Count; i++) if(occ.ContainsKey(seq[i])) occ[seq[i]] = occ[seq[i]] + 1; else occ.Add(seq[i], 1); // find the second largest occurrence int first_max = int.MinValue, sec_max = int.MinValue; foreach(KeyValuePair<String, int> entry in occ) { int v = entry.Value; if( v > first_max) { sec_max = first_max; first_max = v; } else if (v > sec_max && v != first_max) sec_max = v; } // Return string with occurrence equals // to sec_max foreach(KeyValuePair<String, int> entry in occ) { int v = entry.Value; if (v == sec_max) return entry.Key; } return null; } // Driver method public static void Main(String[] args) { String []arr = { "ccc", "aaa", "ccc", "ddd", "aaa", "aaa" }; List<String> seq = new List<String>(arr); Console.WriteLine(secMostRepeated(seq)); } } // This code is contributed by Rajput-Ji |
Output:
ccc
Reference:
https://www.careercup.com/question?id=5748104113422336
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Second most repeated word in a sequence in Python
- Most frequent word in first String which is not present in second String
- Find the first repeated word in a string
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- C program to find and replace a word in a File by another given word
- Find the word from a given sentence having given word as prefix
- Program to find second most frequent character
- Most frequent word in an array of strings
- Find the word with most anagrams in a given sentence
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- k-th missing element in increasing sequence which is not present in a given sequence
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- Convert an unbalanced bracket sequence to a balanced sequence
- Repeated subsequence of length 2 or more
- Queries for characters in a repeated string
- Find the first repeated character in a string
- Smallest length string with repeated replacement of two distinct adjacent
Improved By : Rajput-Ji

