tags:

views:

116

answers:

3

I have some numbers stored in a vector . I want to find which number appears most in the vector.

Is there any easy/fast algorithm (STL or whatever) that does this ?

+3  A: 

Sort it, then iterate through it and keep a counter that you increment when the current number is the same as the previous number and reset to 0 otherwise. Also keep track of what was the highest value of the counter thus far and what the current number was when that value was reached. This solution is O(n log n) (because of the sort).

Alternatively you can use a hashmap from int to int (or if you know the numbers are within a limited range, you could just use an array) and iterate over the vector, increasing the_hashmap[current_number] by 1 for each number. Afterwards iterate through the hashmap to find its largest value (and the key belonging to it). This requires a hashmap datastructure though (unless you can use arrays which will also be faster), which isn't part of STL.

sepp2k
Just to add the obvious, for sorting using STL's sort:http://www.cplusplus.com/reference/algorithm/sort/
Ben Hocking
@Steve314: unordered_map will be in the upcoming standard. It is not in the 2003 (i.e. current) standard.
sepp2k
Wierd - I somehow got the idea that TR1 was the 2003 "first text revision".
Steve314
@Steve314: ISO 14882:2003 is effectively 14882:1998 plus **TC1**, TR1 is just a 'report' and not a corrigendum and hasn't yet made it into a new version of the standard.
Charles Bailey
Ah - so somewhere along the line I confused TC1 with TR1 - that explains a thing or two.
Steve314
You don't need two passes. You can do this in a single pass, keeping track of the largest count "as you go". See code below.
tucuxi
A: 

This is how i did it:

    int max=0,mostvalue=a[0];
    for(i=0;i<a.size();i++)
    {
        co = (int)count(a.begin(), a.end(), a[i]);
        if(co > max)
        {       max = co;
                mostvalue = a[i];
        }
    }

I just don't know how fast it is, i.e. O() ? If someone could calculate it and post it here that would be fine.

VaioIsBorn
It's O(n * n), so not the most efficient method but it is read-only and requires no additional storage if that's important.
Charles Bailey
This is O(n^2), because every time you call count, it looks at every element in the vector. It can be done in O(n) (http://stackoverflow.com/questions/512590/computing-the-statistical-mode/512601#512601)
Matthew Flaschen
A: 

If you want to avoid sorting your vector v, use a map:

int max = 0;
int most_common = -1;
map<int,int> m;
for (vi = v.begin(); vi != v.end(); vi++) {
  m[*vi]++;
  if (m[*vi] > max) {
    max = m[*vi]; 
    most_common = *vi;
  }
}

This requires more memory and has a very similar expected runtime. The memory required should be on the order of a full vector copy, less if there are many duplicate entries.

tucuxi