sjkaliski/numbers.js — pull request #20 by nkohari (2012-12-12 01:21:42+00:00)
Title: A couple of changes to median() and mode()

Hey there, great work on the library! I made a couple changes to the statistical functions that you might be interested in.

I noticed that median() relied on the array being sorted to return the correct result. A couple of the other functions sorted the array before doing their work, but it felt a little dangerous to mutate the array. I altered median() to create a copy of the array and sort that copy instead. I also added a couple of tests to ensure the sorted and unsorted cases were exercised.

For mode(), I changed the algorithm a bit to use a hash of counts. This version will execute in O(2n) at the cost of a bit of RAM to store the hash.