Sort it, then swipe over it and save the counter, which you increase when the current number matches the previous number and reset to 0 otherwise. Also keep an eye on what was the highest counter value so far and what is the current number when that value was reached. This solution is O(n log n) (due to sorting).
Alternatively, you can use hashmap from int to int (or if you know the numbers are in a limited range, you can just use an array) and the_hashmap[current_number] over the vector, increasing the_hashmap[current_number] by 1 for each number. Then iterating through the hash map, you can find its greatest value (and its key). This requires a hashmap data structure, though (if you cannot use arrays, which will also be faster), that is not part of the STL.
sepp2k
source share