There is a class consisting of 'N' students. There can be many students with the same name.
Now, you have to print the names of the students followed by there frequency as shown in the sample explanation given below.
Output the names in the lexicographical order.
Example:
Input: n = 5, {"sumit", "ambuj", "himanshu", "ambuj", "ambuj"}
Output:
ambuj 3 himanshu 1 sumit 1
Approach:
C++
#include <bits/stdc++.h>using namespace std;void frequencyOfStudent(int n, vector<string> &arr){map<string, int> mp;for (int i = 0; i < n; i++){string s = arr[i];mp[s]++;}vector<pair<string, int>> v;for (auto it = mp.begin(); it != mp.end(); it++)v.push_back({it->first, it->second});sort(v.begin(), v.end());for (int i = 0; i < v.size(); i++)cout << v[i].first << " " << v[i].second << "\n";}int main(){int n = 5;vector<string> arr = {"sumit", "ambuj", "himanshu","ambuj", "ambuj"};frequencyOfStudent(n, arr);return 0;}
No comments:
Post a Comment