Problem
Please suggest an efficient method.
I have been asked this question in the written examination for placement in a company.
Write an program to find the top 5 most frequently occuring number in an array
What have you got so far?
I used count method in the exam
But that method doesn't work if the number appears less than 50%
Max heap can be used for this.
Well I can't say if this is the most efficient method. But it make heavy use of the standard libs so its probably not too bad:
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
|
#include <set>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
// Duplicate range
typedef std::pair<int*, int*> dups;
// Sort multiset based on number of duplicates
struct fewer
{
bool operator()(const dups& a, const dups& b)
{
return a.second - a.first < b.second - b.first;
}
};
typedef std::multiset<dups, fewer> dupset;
typedef std::multiset<dups, fewer>::reverse_iterator riter;
void top_freq(int* array, int size, int number)
{
dupset sorted;
int* i = array;
int* end = array + size;
// Sort array to make duplicates adjacent
std::sort(i, end);
// Put duplicate ranges into multiset
dups p;
while(i != end)
{
p = std::equal_range(i, end, *i);
sorted.insert(p);
i = p.second;
}
// Output the top number of elements
riter r = sorted.rbegin();
for(int i = 0; r != sorted.rend() && i < number; ++r, ++i)
{
std::cout << *r->first << " has " << (r->second - r->first) << '\n';
}
}
const int SIZE = 1024;
int main()
{
srand(time(0));
int array[SIZE];
for(int i = 0; i < SIZE; ++i)
{
array[i] = rand() % 100;
}
top_freq(array, SIZE, 5);
}
|
Last edited on
Topic archived. No new replies allowed.