<?php
$array = array(6,8,9,8,9,8,5,4,4,9);
function Counting($array){
$freq_data = array_count_values($array);
$unique_elements = array_map('intval',array_keys($freq_data));
$min_element = min($unique_elements);
$max_element = max($unique_elements);
$percentage_data = [];
$total = count($array);
for($i = $min_element; $i <= $max_element ; ++$i){
$current_percentage = isset($freq_data[$i]) ? ($freq_data[$i] / $total) : 0;
$percentage_data[$i] = $i > $min_element ? $percentage_data[$i-1] + $current_percentage : $current_percentage;
}
uksort($percentage_data,function($a,$b){
return strnatcmp($a,$b);
});// just to be sure that associative hash keys are in ascending order
return $percentage_data;
}
print_r(Counting($array));
preferences:
114.5 ms | 412 KiB | 5 Q