Mode of a set of data

An exercise in my textbook asks me to "Create a program that finds the mode of a set of positive integers." I am using user input to get the set of integers and storing them in a vector with push_back(). I am then using sort to arrange them from least to greatest, and getting the same integers next to each other in the vector. Here is what I have so far.


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
//I know I include more than I use, but my textbook 
//tells me to include it for now, and it will explain 
//when I need what later. 
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
//takes numbers and finds mode
int main()
{
  vector<int> numbers;
  int numberBuffer;

  //enters data into vector
  while(cin >> numberBuffer) {
    numbers.push_back();
  }

  //sorts
  sort(numbers.begin(), numbers.end());
  
  return 0;
}


What I cannot figure out is how to analyze the data to find the mode. Any ideas would be great. Thanks in advance!

And as always, comments on my coding style and readability are welcome.
Last edited on
The mode is the data value that appears most often.

Since you have sorted the data, your life gets easier. For each unique value, count how often it appears. I recommend you use a std::map to keep track of (value, count). Copy your data to a vector, sorting on 'count'. Then print the values whose count are equal to the largest count.

You can also use a multimap to do the same kind of thing, except swapping keys and values. The largest key has the values to print. You have to know how to use your multimap container, though.

If you do use a map, you can forget the sorting step, since the map takes care of that for you when you add elements...

Good luck!
Is there a way to do this without a map? My book hasn't taught it yet, so I assume there might be a method to do it without a map?
Last edited on
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.
Ah! This does help thank you. Though to find the largest value wouldn't you just sort the values and then the last element in the vector would be the largest?
As it applies to your problem, no. (That's the map method.)

Because you are trying to sort on not the value, but the number of times the value appears in the array.

(The histogram in a counting sort is essentially a map, which you can use to find the mode...)
Thank you. This has helped a lot.
Topic archived. No new replies allowed.