How do I find the mode of a sorted array?

前端 未结 4 822
囚心锁ツ
囚心锁ツ 2021-01-20 15:41

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

相关标签:
4条回答
  • 2021-01-20 16:09

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-20 16:13

    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;
    }
    
    0 讨论(0)
  • 2021-01-20 16:26
    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".

    0 讨论(0)
提交回复
热议问题