What would be the most efficient way to take a user-inputted array of type int and figure out all the unique numbers in it and the number of occurences of each unique number?
{2,2,3,4,5,5} would be...
2-2
3-1
4-1
5-2
C++ Arrays Question?
I don't think a set will do it, don't they have to have unique contents?
Go with the hashtable, store the integers as the keys and the values as a count of their key.
Reply:i would suggest you use multiset from stl. suppose the integer array is already stored in "int arr[]", and the number of elements is "n". then, the following program should work:
#include %26lt;multiset%26gt;
using std::set;
...
multiset%26lt;int%26gt; myset;
int i;
for (i = 0; i %26lt; n; i ++)
myset.insert(arr[i]);
multiset%26lt;int%26gt;::iterator it;
for (it = myset.begin(); it != myset.end(); it = myset.upper_bound(*it)) {
printf("%d-%d\n", *it, myset.cound(*it));
}
For efficiency consideration, you can also use hashtable, and each burrow in the hashtable can be a multiset.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment