The work consists in creating 2 arrays with random numbers between 0 and 30. These 2 arrays should be drawn from the most common element to the least common one.
Then I have to show the most common, less common element to the screen, and which numbers from 0 to 30 are missing in both arrays.
At this moment I can show the most common element to the screen, my question is how to find out the least common.
I've done research on the various functions of sort (arsort, asort, krsort, etc) but still I could not proceed.
The code I have is the following:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>
Totally not a legit website
</title>
</head>
<body>
<?php
function veto($min, $max) {
for ($i = 0; $i < 10; $i++){
$array1[$i] = rand($min, $max);
$array2[$i] = rand($min, $max);
}
// print_r($array1);
// print_r($array2);
$count_1 = array_count_values($array1);
$count_2 = array_count_values($array2);
arsort($count_1); // sortear
arsort($count_2); // descendente
$first_array1 = key($count_1); //mais repetido do array1
$first_array2 = key($count_2); //mais repetido do array2
//------------------------------------------------------------------------------------------------
$count_first_1 = current($count_1);
$count_second_1 = next($count_1);
if($count_first_1 != $count_second_1) { // para saber se existem repetidos no 1º lugar
echo $first_array1 . ' mais comum no primeiro array';
echo "</br>";
} else {
echo $first_array1 . ' - input repetido no mais comum';
echo "</br>";
}
//-------------------------------------------------------------------------------------------------
$count_first_2 = current($count_2);
$count_second_2 = next($count_2);
if($count_first_2 != $count_second_2) { // para saber se existem repetidos no 1º lugar
echo $first_array2 . ' mais comum no segundo array';
echo "</br>";
} else {
echo $first_array2 . ' - input repetido no mais comum';
echo "</br>";
}
print_r ($array1);
echo "</br>";
print_r ($array2);
echo "</br>";
// print_r(end($count_1));
// echo "</br>";
// echo print_r(end($count_2));
// echo "</br>";
}
$min = 0;
$max = 30;
veto($min, $max);
?>
</body>
</html>
Thank you in advance.