Mode calculator

Calculates and returns the mode of the numbers given


function mode($numbers=array())
	if (!is_array($numbers))
		$numbers = func_get_args();
	$amt = array_count_values($numbers);

	foreach ($amt as $key => $val)
		if ($val == max($amt)) return $key;

echo mode(array(2,2,3,3,4,5)); // 2
echo mode(4,4,3,3,3,2,4,5); // 4



As seen in the commented portion of code, this function can take either an array of numbers, or multiple parameters of numbers. *note* returns the "first" key that is a mode, only one is returned regardless of uniqueness.


Add your comment