mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-16 22:58:14 +01:00
22 lines
487 B
PHP
22 lines
487 B
PHP
<?php
|
|
|
|
/**
|
|
* This function calculates
|
|
* the mode value(s) of
|
|
* numbers provided
|
|
*
|
|
* @param decimal $numbers A variable sized number input
|
|
* @return array $modes Array of mode(s) of provided numbers
|
|
* @throws \Exception
|
|
*/
|
|
function mode(...$numbers)
|
|
{
|
|
if (empty($numbers)) {
|
|
throw new \Exception('Please pass values to find the mode');
|
|
}
|
|
|
|
$values = array_count_values($numbers);
|
|
$maxCount = max($values);
|
|
return array_keys($values, $maxCount, true);
|
|
}
|