Sure, just start counting.
The principle is very much the same as the find the largest/smallest element in an array problem. You'll need a variable to store the current largest count and one for the associated value (or values), and then one for the current value and one for the current count.
For reference, here's the "find largest value" problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
vector <int> values;
// (populate values here...) //
int largest_value = values[ 0 ];
for (unsigned n = 1; n < values.size(); n++)
{
if (values[ n ] > largest_value)
{
largest_value = values[ n ];
}
}
cout << "largest value is " << largest_value << ".\n";
|
Remember, you've already sorted the values in the vector, so your vector will look something like:
1, 2, 2, 3, 3, 3, 3, 4, 5, 5
I've added extra space to visually separate the runs of values. You can see that there is one 1, two 2s, four 3s, one 4, and two 5s.
Keep a count of how many items there are before the number changes. When the number does change (or hits the end of the vector), compare it to the previous count of most numbers. If larger, make note of the number and count, just as above we made note of
largest_value.
If you think it possible that there may be more than one mode, keep the value in a separate vector.
1 2
|
vector <int> mode_values;
unsigned largest_count = 0;
|
Hope this helps.