How to calculate or approximate the median of a list without storing the list

If the values are discrete and the number of distinct values isn’t too high, you could just accumulate the number of times each value occurs in a histogram, then find the median from the histogram counts (just add up counts from the top and bottom of the histogram until you reach the middle). Or if they’re continuous values, you could distribute them into bins – that wouldn’t tell you the exact median but it would give you a range, and if you need to know more precisely you could iterate over the list again, examining only the elements in the central bin.

Leave a Comment