# Finding the mode of an array

• 05-01-2007
Crcullen3916
Finding the mode of an array
I am trying to find the mode, or the number which occurs most often, of my array. This is the scores array, which is sorted:

Code:

```        820         987         1000         1020         1100         1200         1450         1550         1680         2352         2400         2400         2400```
So far I have gotten the program to count how many times there is a duplicate, which is 3 because 2400 occurs 3 times. But I haven't gotten the program to print out which of the numbers occurs 3 times.
Here is my incomplete code:

Code:

```int mode (int scores [], int n) {     int counter = 0;     for (int pass = 0; pass < n - 1; pass++)           for (int count = pass + 1; count < n; count++) {               if (scores [count] == scores [pass])               counter++;                        }           file_out << counter; }```
How can I modify this so that it will tell me that 2400 occurs 3 times?
• 05-01-2007
laserlight
Your current code does not appear to find the mode, since you do not compare the current count with the current maximum frequency count.

A simple solution would be to use a std::map<int, int> to map the numbers to their frequencies. Once you have inserted the numbers, you can use std::max_element() to find the maximum frequency and from there print out both the mode and its frequency.
• 05-01-2007
i dont think your algorithm for getting the number of occurrences is correct. add a few more numbers, so that the mode is somewhere in the middle and you should notice.

ive never written one, but i assume it would be a more complicated algorithm than that to find the maximum number of occurrences, and once you have that i imagine it would be straightforward to find out which one it was.

post back with your corrected code for doing the same task. (unless im wrong, of course!)
• 05-01-2007
Crcullen3916
I haven't learned maps yet. I looked it up and it's way past my level. I think I'll wait on this one, maybe talk to my professor, it's optional anyway.
• 05-01-2007
laserlight
Well, you do not need std::map to find the mode. With a sorted array, counting frequencies is pretty easy, and you seem to have the right idea. What you need to do is keep track of what is the current maximum frequency and its associated number. Think along the lines of what you would do if you have to implement an algorithm to find the maximum value in an array, except now you do not just need to keep track of the value, but also its key.
• 05-02-2007
CornedBee
Algorithm works pretty much like this:
Code:

```var currentMaxFreq = 0, currentMode = 0; var currentFreq = 0, currentNum = -1; for each num in sortedArray {   if currentNum != num {     if currentFreq > currentMaxFreq {       currentMaxFreq = currentFreq;       currentMode = currentNum;     }     currentFreq = 0;     currentNum = num;   }   ++currentFreq; }```
What's missing is being a bit more careful at the start and handling the last number.