Skip to main content
Module

x/rambda/median.js

Faster and smaller alternative to Ramda
Go to Latest
File
import { mean } from './mean'
export function median(list){ const len = list.length if (len === 0) return NaN const width = 2 - len % 2 const idx = (len - width) / 2
return mean(Array.prototype.slice .call(list, 0) .sort((a, b) => { if (a === b) return 0
return a < b ? -1 : 1 }) .slice(idx, idx + width))}