Can ne1 tell me the algotithm to calculate the mode of an int list. The numbers are already sorted!
Printable View
Can ne1 tell me the algotithm to calculate the mode of an int list. The numbers are already sorted!
As long as you can be sure that your list is ordered this will work for unimodal data and will give one of the modes for multimodal data (I'm sure you can make it more genaral :) ).
1) Start by saying that the mode is the first element of the list and that the mode count is one.
2) While the current element is equal to the previous one step through the list incrementing the count
2a) If the count is greater than for any other number, then the current number is the new mode.
3) if the current number is different from the last one reset the count.
4) Once you reach the end of the list you know the mode...