# Find The Mode

Computer Science Level 2

Finding the mode of a few numbers isn't too hard, but if you have a very long list, you'll probably need some code!

To find the mode, you use the following:

  1 2 3 4 5 6 7 8 9 10 11 12 current_count = 0 mode_count = 0 for position in range(len(array)): if array[position] == array[position-1]: current_count += 1 if array[position] != array[position-1] \ or position == len(array)-1: if current_count > mode_count: mode = array[position-1] mode_count = current_count current_count = 1 print mode 

For this approach to be successful, which of the following must be true about your array?

I. It is sorted.

II. It is unimodal.

Details

• The mode is defined as the element which appears most often in a set. If more than one element appears most often, the mode is that set of elements which appear most often.
×

Problem Loading...

Note Loading...

Set Loading...