Latest

6/recent/ticker-posts

FIND K MOST FREQUENT NUMBERS IN A SET OF NUMBERS

 #include <bits/stdc++.h>

#include<map>

using namespace std;


int main()

{   int a[10]={6,8,8,7,7,9,1,4,8,18};    //take user input array of size n

    int k=2;                  //no of  elements with high occurance you want to find

    unordered_map<int,int>mp;

    priority_queue <pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>minheap;

    for(int i=0;i<10;i++)

    {   mp[a[i]]++;

    }

    for(auto it=mp.begin();it!=mp.end();it++)

    {

        minheap.push(make_pair(it->second,it->first));

        if(minheap.size()>k)

            minheap.pop();

    }

    while(minheap.size()>0)

    {

        cout<<minheap.top().second<<endl;

        minheap.pop();

    }


    return 0;

}

    output:

    7

    8




Post a Comment

1 Comments