-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_MostFreqWordInArrayOfStrings.cpp
63 lines (52 loc) · 1.6 KB
/
GFG_MostFreqWordInArrayOfStrings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
https://practice.geeksforgeeks.org/problems/most-frequent-word-in-an-array-of-strings3528/1#
Most frequent word in an array of strings
*/
// { Driver Code Starts
// Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function template for C++
class Solution
{
public:
//Function to find most frequent word in an array of strings.
string mostFrequentWord(string arr[], int n)
{
// code here
unordered_map<string, int> freq; // store frequency of each word
unordered_map<string, int> order; // store arrving order of each string
int maxFreq=INT_MIN;
string maxString = "";
for(int i=0; i<n; i++)
{
int f = freq[arr[i]]++;
if(order.count(arr[i]) == 0) //returns the number of elements matching specific key
order[arr[i]] = i;
// if frequency of current string is greater than maxFreq OR
// if they have same freq then maximize according to order of arrival acc to question
if(f > maxFreq || maxFreq == f && i > order[maxString])
{
maxFreq = f;
maxString = arr[i];
}
}
return maxString;
} //end
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
Solution obj;
cout << obj.mostFrequentWord(arr, n) << endl;
}
return 0;
}
// } Driver Code Ends