Frequency of Students

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 nvector<string&arr)
{
    map<stringintmp;
    for (int i = 0i < ni++)
    {
        string s = arr[i];

        mp[s]++;
    }
    vector<pair<stringint>> v;
    for (auto it = mp.begin(); it != mp.end(); it++)
        v.push_back({it->firstit->second});
    sort(v.begin(), v.end());
    for (int i = 0i < v.size(); i++)
        cout << v[i].first << " " << v[i].second << "\n";
}
int main()
{
    int n = 5;

    vector<stringarr = {"sumit""ambuj""himanshu",
                          "ambuj""ambuj"};

    frequencyOfStudent(narr);

    return 0;
}


No comments:

Post a Comment