php 如何在不使用max函数的情况下找到数组中的最高和第二大数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26448786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:54:26  来源:igfitidea点击:

how to find highest and second highest number in an array without using max function

phparrays

提问by Lakhan

I already have solution. But i think it will be more optimizable. So please provide me a solution for it. And remember that don't use predefined function of php. Like max() function. i know there are so many ways to find it but i want best and better solution. Because my array contains more than 1 lakh records and it's taking lot of time. Or sometime site will be down.

我已经有了解决方案。但我认为它会更优化。所以请给我一个解决方案。并记住不要使用 php 的预定义函数。像 max() 函数。我知道有很多方法可以找到它,但我想要最好和更好的解决方案。因为我的数组包含超过 10 万条记录,而且需要花费大量时间。或者有时网站会关闭。

My code :

我的代码:

<?php 

$array = array('1', '15', '2','10',4);

echo "<pre>";
print_r($array);
echo "<pre>";
$max = 0;
$s_max=0;

for($i=0; $i<count($array); $i++)
{
    $a = $array[$i];
    $tmax = $max;
    $ts_max = $s_max;
    if($a > $tmax && $a > $ts_max)
    {
        $max = $a;
        if($tmax > $ts_max) {
            $s_max = $tmax;
        } else {
            $s_max = $ts_max;
        }
    } else if($tmax > $a && $tmax > $ts_max)
    {
        $max = $tmax;
        if($a > $ts_max) {
            $s_max = $a;
        } else {
            $s_max = $ts_max;
        }
    } else if($ts_max > $a && $ts_max > $tmax)
    {
        $max = $ts_max;
        if($a > $tmax)
        {
            $s_max = $a;
        } else {
            $s_max = $tmax;
        }
    }
}
echo "Max=".$max;
    echo "<br />";
    echo "S_max=".$s_max;
    echo "<br />";

?>

回答by Kanishka Panamaldeniya

<?php 
$array = array('200', '15','69','122','50','201');
$max_1 = $max_2 = 0;

for ($i=0; $i<count($array); $i++) {
    if ($array[$i] > $max_1) {
      $max_2 = $max_1;
      $max_1 = $array[$i];
    } else if ($array[$i] > $max_2 && $array[$i] != $max) {
      $max_2 = $array[$i];
    }
}
echo "Max=".$max_1;
echo "<br />"; 
echo "Smax 2=".$max_2;

回答by user2610558

See this solution.

请参阅此解决方案。

<?php 

 $numbers = array_unique(array(1,15,2,10,4)); 
// rsort : sorts an array in reverse order (highest to lowest).

 rsort($numbers); 

 echo 'Highest is -'.$numbers[0].', Second highest is -'.$numbers[1];

 // O/P: Highest is -15, Second highest is -10
 ?>

回答by Markus Malkusch

I didn't check your solution, but in terms of complexity it's IMO optimal. If the array has no more structural information (like it's sorted) there's no way to skip entries. I.e. the best solution is in O(n) which your solution is.

我没有检查您的解决方案,但就复杂性而言,它是 IMO 最佳的。如果数组没有更多的结构信息(如已排序),则无法跳过条目。即最好的解决方案是在 O(n) 中,您的解决方案是。

回答by gopal sharma

The answer given by "Kanishka Panamaldeniya" is fine for highest value but will fail on second highest value i.e. if array has 2-similar highest value, then it will showing both Highest and second highest value same. I have sorted out it by adding one more level comparsion and it works fine.

“Kanishka Panamaaldeniya”给出的答案适用于最高值,但在第二高值时会失败,即如果数组具有两个相似的最高值,那么它将显示最高值和第二高值相同。我通过添加更多级别的比较对其进行了整理,并且效果很好。

$array = array(50,250,30,250,40,70,10,50); // 250  2-times
$max=$max2=0;
for ($i = 0; $i < count($array); $i++) {
if ($array[$i] > $max) {
    $max2 = $max;
    $max = $array[$i];
} else if (($array[$i] > $max2) && ($array[$i] != $max)) {
    $max2 = $array[$i];
}
}
echo "Highest Value is : " . $max . "<br/>"; //output : 250
echo "Second highest value is : " . $max2 . "<br/>"; //output : 70

回答by Abhay deep singh

This code will return second max value from array

此代码将从数组中返回第二个最大值

$array = array(80,250,30,250,40,90,10,50,60,50); // 250  2-times
$max=$max2=0;

for ($i = 0; $i < count($array); $i++) {

    if($i == 0) {

        $max2 = $array[$i];
    }

    if($array[$i] > $max) {

        $max = $array[$i];

    }

    if($max > $array[$i] && $array[$i] > $max2) {

        $max2 = $array[$i];
    }



   }    


echo "Highest Value is : " . $max . "<br/>"; //output : 250
echo "Second highest value is : " . $max2 . "<br/>"; //output : 90

回答by Pankaj Raj

$array = array(80,250,30,40,90,10,50,60,50); // 250  2-times
$max=$max2=0;

foreach ($array as $key =>$val) {

    if($max < $val) {
        $max2 =$max;
        $max = $val;        
    }

    elseif(($max2 < $val) && ($max!=$val) {
        $max2 = $val;
    }
}

echo "Highest Value is : " . $max . "
"; //output : 250 echo "Second highest value is : " . $max2 . "
"; //output : 90

回声“最高值是:”。$max 。“
”; //输出 : 250 echo "第二高的值是:" . $max2 。“
”; //输出:90

回答by Sr. PHP Programmer Team Lead

This is a perfect and shortest code to find out the second largest value from the array. The below code will always return values in case the array contains only a value.

这是从数组中找出第二大值的完美且最短的代码。如果数组只包含一个值,下面的代码将始终返回值。

Example 1.
    $arr = [5, 8, 1, 9, 24, 14, 36, 25, 78, 15, 37];
    asort($arr);
    $secondLargestVal = $arr[count($arr)-1];

    //this will return 37

Example 2.

    $arr = [5];
    asort($arr);
    $secondLargestVal = $arr[count($arr)-1];
    //this will return 5