3

I need to write a function to find the mode of a array. I'm not good at coming up with algorithms however and I'm hoping someone else knows how to do this.

I know the size of the array and the values in each element, and I have the array sorted from least to greatest.

array would be passed to the mode function like

mode = findMode(arrayPointer, sizePointer);

UPDATE:

After reading the comments I've tried this

int findMode(int *arrPTR, const int *sizePTR)
{
    int most_found_element = arrPTR[0];
    int most_found_element_count = 0;
    int current_element = arrPTR[0];
    int current_element_count = 0;
    int count;
    for (count = 0; count < *sizePTR; count++)
    {
        if(count == arrPTR[count])
             current_element_count++;
        else if(current_element_count > most_found_element)
        {
            most_found_element = current_element;
            most_found_element_count = current_element_count;
        }
        current_element = count;
        current_element_count=1;
    }

    return most_found_element;
}

I'm still having problems grasping this algorithm though if anyone can sort me out. I've never used vectors so don't really understand the other examples.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
sircrisp
  • 1,037
  • 4
  • 17
  • 32
  • 3
    can you describe some things you tried and maybe post some code? Here's a hint to you: you might try to loop through the array, and each time you see an element, increment an element-specific counter by one. – asf107 Feb 16 '12 at 17:49
  • 4
    You want the _mode_ of an array? You mean _mean_? Or _median_? Or do I need to learn a new term? EDIT: [Mode is a real thing! I R LERND](http://www.mathsteacher.com.au/year8/ch17_stat/02_mean/mean.htm) – Mooing Duck Feb 16 '12 at 17:52
  • 2
    @wilhelmtell- most repeated element of an array – sgowd Feb 16 '12 at 17:52
  • As a related question, see http://stackoverflow.com/questions/5438786/finding-mode-of-vector-of-ints-in-c. The histogram answer (http://stackoverflow.com/a/5439001/96780) is worth noting; it works even if the array is not sorted. – Daniel Daranas Feb 16 '12 at 17:56

4 Answers4

7

You have almost everything.

You can take advantage of the fact that the array is sorted.

Just go through the array keeping track of both the current equal consecutive numbers, and the greatest number of equal consecutive numbers you have found until that point (and which number produced it). In the end you will have the greatest number of equal consecutive numbers and which number produced it. That will be the mode.

Note: For a solution which does not require the array to be sorted, see for example one based in the histogram approach in a related question.

Community
  • 1
  • 1
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
5
set most_found_element to the first element in the array
set most_found_element_count to zero
set current_element to the first element of the array
set current_element_count to zero
for each element e in the array
    if e is the same as the current_element
        increase current_element_count by one
    else
        if current_element_count is greater than most_found_element_count
            set most_found_element to the current_element
            set most_found_element_count to current_element_count
        set current_element to e
        set current_element_count to one
if current_element_count is greater than most_found_element_count
    set most_found_element to the current_element
    set most_found_element_count to current_element_count
print most_found_element and most_found_element_count

I thought the names would explain it, but here we go:

When we start, no element has been found the most times
  so the "high-score" count is zero.
Also, the "current" value is the first, but we haven't looked at it yet 
  so we've seen it zero times so far
Then we go through each element one by one
  if it's the same as "current" value, 
     then add this to the number of times we've seen the current value.
  if we've reached the next value, we've counted all of the "current" value.
     if there was more of the current value than the "high-score"
        then the "high-score" is now the current value
     and since we reached a new value
        the new current value is the value we just reached
Now that we've seen all of the elements, we have to check the last one
  if there was more of the current value than the "high-score"
    then the "high-score" is now the current value
Now the "high-score" holds the one that was in the array the most times!

Also note: my original algorithm/code had a bug, we have to do an extra check of "current" after the loop ends, as it never finds "the one after the last".

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • I tried and posted the code in the question but am still having trouble understanding this. Could you tell me what I'm doing wrong? – sircrisp Feb 16 '12 at 20:36
  • @sircrisp: There was a bug in my algorithm that I addressed if the highest value was the mode. Also, I added an explanation. – Mooing Duck Feb 17 '12 at 01:49
3

Hints:

Q: How do you define the mode?

A: The number whose count is greatest within the array.

Q: How do you count numbers in an ordered array?

A: Iterate through the array, and while the next item is equal to the previous, increment the count for that value.

Q: If the count of the previous value is less than the count of the current value, then can the previous value be the mode?

A: No

NominSim
  • 8,447
  • 3
  • 28
  • 38
0

If the input array is sorted, here is the same approach as described in other answers but implemented in a different way, a better and easy to understand way.

  1. Run a loop over the input array.
  2. Keep global mode value and mode count.
  3. Run a sliding window till you find equal elements.
  4. If local equal element count is greater than global mode count, then update global mode and mode count.

Here is working and tested code in C++.

int mode(vector<int> a, int N)
{
    int mode = a[0];
    int mode_count = 1;
    int i = 0;
    while (i < N - 1) {
        int cur = a[i];
        int cur_count = 1;
        while (a[i] == a[i + 1]) {
            i++;
            cur_count++;
        }
        if (cur_count > mode_count) {
            mode_count = cur_count;
            mode = a[i];
        }
        i++;
    }
    return mode;
}
Rajendra Uppal
  • 19,218
  • 15
  • 59
  • 57