Skip to content

Data Science

Mean, Median, and Mode

Finding the mean or mode takes one pass through the data — O(n) time. Finding the median needs the data sorted first, so it takes O(n log n) time, unless the data is already sorted.

The idea, in plain English

Picture a seesaw with all your numbers sitting on it. The mean, or 'average', is the exact spot where the seesaw balances. Big numbers pull it one way. Small numbers pull it the other way. The median works differently. Line up your numbers from smallest to largest. The median is just the number standing in the middle. The mode is the simplest of the three. It is whichever number shows up the most, like the most popular answer in a room.

How it works

  1. 1Add up every number, then divide the total by how many numbers you have. This gives you the mean.
  2. 2Sort your numbers from smallest to largest. Pick the number sitting exactly in the middle. That is the median. If there are two middle numbers, average them.
  3. 3Count how many times each number appears. The number that appears most often is the mode.

When you'd use it

Use the mean for a quick overall average. Use the median instead when a few extreme values, like one billionaire's income, would badly skew the mean. Use the mode when you care about the single most common category, like the most popular shoe size.

Common beginner mistakes

  • Do not trust the mean when your data has outliers. One huge or tiny value can drag it far from what feels 'typical'.
  • Do not forget to sort your numbers before finding the median. A 'middle' value from unsorted data means nothing.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
Scores: 12 7 7 15 9
Mean: 10.00
Median: 9
Mode: 7

Not sure this is the right topic? See the learning paths → or where this leads →