2022-10-16 01:41:56 -03:00
|
|
|
<?php
|
2023-10-04 21:36:11 +03:30
|
|
|
|
2022-10-16 01:41:56 -03:00
|
|
|
/**
|
|
|
|
* This function calculates
|
2023-09-16 16:45:18 +03:30
|
|
|
* the mode value(s) of
|
2022-10-16 01:41:56 -03:00
|
|
|
* numbers provided
|
2023-09-16 16:45:18 +03:30
|
|
|
*
|
2022-10-16 01:41:56 -03:00
|
|
|
* @param decimal $numbers A variable sized number input
|
2023-09-16 16:45:18 +03:30
|
|
|
* @return array $modes Array of mode(s) of provided numbers
|
|
|
|
* @throws \Exception
|
2022-10-16 01:41:56 -03:00
|
|
|
*/
|
|
|
|
function mode(...$numbers)
|
|
|
|
{
|
|
|
|
if (empty($numbers)) {
|
2023-09-16 16:45:18 +03:30
|
|
|
throw new \Exception('Please pass values to find the mode');
|
2022-10-16 01:41:56 -03:00
|
|
|
}
|
|
|
|
|
2023-09-16 16:45:18 +03:30
|
|
|
$values = array_count_values($numbers);
|
|
|
|
$maxCount = max($values);
|
|
|
|
return array_keys($values, $maxCount, true);
|
2023-10-04 21:36:11 +03:30
|
|
|
}
|