给定一个字符串序列,任务是找出给定序列中重复次数第二(或最频繁)的字符串。(考虑到没有两个单词是重复次数第二多的,所以总会有一个单词)。
null
例如:
Input : {"aaa", "bbb", "ccc", "bbb", "aaa", "aaa"}Output : bbbInput : {"geeks", "for", "geeks", "for", "geeks", "aaa"}Output : for
问:亚马逊
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 |
Python3
# Python3 program to find out the second # most repeated word # Function to find the word def secMostRepeated(seq): # Store all the words with its occurrence occ = {} for i in range ( len (seq)): occ[seq[i]] = occ.get(seq[i], 0 ) + 1 # Find the second largest occurrence first_max = - 10 * * 8 sec_max = - 10 * * 8 for it in occ: if (occ[it] > first_max): sec_max = first_max first_max = occ[it] elif (occ[it] > sec_max and occ[it] ! = first_max): sec_max = occ[it] # Return with occurrence equals # to sec_max for it in occ: if (occ[it] = = sec_max): return it # Driver code if __name__ = = '__main__' : seq = [ "ccc" , "aaa" , "ccc" , "ddd" , "aaa" , "aaa" ] print (secMostRepeated(seq)) # This code is contributed by mohit kumar 29 |
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 |
Javascript
<script> // JavaScript program to find out the second // most repeated word // Function to find the word function secMostRepeated(seq) { // Store all the words with its occurrence let occ = new Map(); for (let i = 0; i < seq.length; i++) { if (occ.has(seq[i])){ occ.set(seq[i], occ.get(seq[i])+1); } else occ.set(seq[i], 1); } // find the second largest occurrence let first_max = Number.MIN_VALUE, sec_max = Number.MIN_VALUE; for (let [key,value] of occ) { if (value > first_max) { sec_max = first_max; first_max = value; } else if (value > sec_max && value != first_max) sec_max = value; } // Return string with occurrence equals // to sec_max for (let [key,value] of occ) if (value == sec_max) return key; } // Driver program let seq = [ "ccc" , "aaa" , "ccc" , "ddd" , "aaa" , "aaa" ]; document.write(secMostRepeated(seq)); // This code is contributed by shinjanpatra </script> |
输出:
ccc
参考: https://www.careercup.com/question?id=5748104113422336 本文由 萨希尔·查布拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END