从一个数组中找出众数,及其出现的次数

$arr = [1, 4, 3, 3, 3, 4, 4, 4];
$maxCount = [];
$len = count($arr);
$zhongshu = '';
for($i=0; $i<$len; $i++)
{
    $count = 0;
    for($j=0; $j<$len; $j++)
    {
        if($arr[$i] == $arr[$j])
        {
            $count++;
            $maxCount[$arr[$i]] = $count ; 
        }
    }
}


$cishu = max($maxCount);
if($cishu > 1 )
{
    foreach($maxCount as $k => $v)
    {
        if($v == $cishu)
        {
            $zhongshu = $k;
        }

    }
}else
{
    $zhongshu = '无众数';
    $cishu = '值出现的个数都唯一!';
}

echo '
';
print_r($maxCount);
echo '
'
; echo '
'
.'众数:' . $zhongshu . '
'
; echo '
'
.'出现次数:' . $cishu ;

你可能感兴趣的:(PHP,算法)