How do I find the mode of a sorted array?

前端 未结 4 861
囚心锁ツ
囚心锁ツ 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

    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

提交回复
热议问题