$max) { $secondMax = $max; $max = $number; } //If array number is greater than secondMax and less than max if($number > $secondMax && $number < $max) { $secondMax = $number; } } return $secondMax; } $arr = array(70, 4, 8, 10, 14, 9, 7, 6, 5, 3, 2); $second_maximum = findSecondMax($arr); echo "Second Highest Element is ".$second_maximum; ?>
Showing posts with label Php -Interview Question. Show all posts
Showing posts with label Php -Interview Question. Show all posts
02 January, 2021
PHP Code to Find Second Largest Number in an Array
Programing Coderfunda January 02, 2021 Php -Interview Question No comments
$max) {
$secondMax = $max;
$max = $number;
}
//If array number is greater than secondMax and less than max
if($number > $secondMax && $number < $max) {
$secondMax = $number;
}
}
return $secondMax;
}
$arr = array(70, 4, 8, 10, 14, 9, 7, 6, 5, 3, 2);
$second_maximum = findSecondMax($arr);
echo "Second Highest Element is ".$second_maximum;
?>