How to find the second largest number in PHP

Let's first understand the problem. We have an array of numbers and need to find the second largest number from that array in PHP.

%0 node_1 7 node_2 2 node_3 8 node_1680080957674 14 node_1680081024856 6 node_1680081036240 3 node_1680080969959 18 node_1680080975113 11 node_1680080973375 4 node_1680080961311 8
14 is the second largest number in this array

We can find the second largest number in an array through multiple approaches.

Approach 1

In this approach, we first sort the array in ascending order and then return the element of the second last index. This approach only works if the elements in the array are unique.

In case there are multiple instances of the maximum value, this approach would fail.

Code example

<?php
function findSecondLargest(array $arr){
//sort the array in ascending order
sort($arr);
//save the element from the second last position of sorted array
$secondLargest = $arr[sizeof($arr)-2];
//return second-largest number
return $secondLargest;
}
//create an array of numbers
$arr = array(7,2,8,14,6,3,18,11,4,5);
//call the function and print output
echo "Second-largest number : ".findSecondLargest($arr);
?>

Code explanation

Line 2: We declare a function named findSecondLargest that accepts an array $arr.

Line 4: We call the sort function to sort the array $arr in ascending order.

Line 6: We store the second-to-last element of the sorted array in the $secondLargest variable.

Line 8: We return the second largest number that's stored in the $secondLargest variable.

Line 12: We declare an array $arr of numbers.

Line 15: We call the findSecondLargest function and print the second largest number returned by this function.

The time complexity of this approach is O(nlogn)O(nlogn) because the built-in sort() function in PHP takes O(nlogn)O(nlogn) where nn is the number of elements in the array $arr.

Now let's see approach 2 to reduce the time complexity and tackle the repetitive elements in the array.

Approach 2

In this approach, we traverse a largest and a secondLargest variable in our array storing the largest and second largest values, respectively.

Code example

<?php
function findSecondLargest(array $arr) {
//If array is empty then return
if(empty($arr)) {
return;
}
//Initialize largest and second largest with negative values
$largest = PHP_INT_MIN;
$secondLargest = PHP_INT_MIN;
//Traverse the whole array
foreach($arr as $num) {
//If element is greater than the value of current largest
if($num > $largest) {
//update largest and second largest
$secondLargest = $largest;
$largest = $num;
}
//If element is greater than secondLargest and less than largest
if($num > $secondLargest && $num < $largest) {
//update second largest
$secondLargest = $num;
}
}
//return second largest value of array
return $secondLargest;
}
//create an array of numbers
$arr = array(7,2,8,14,6,3,18,11,4,5);
//call the function and print output
echo "Second-largest number : ".findSecondLargest($arr);
?>

Code explanation

Line 5–7: We return if the array $arr is empty.

Line 10–11: We declare two variables and initialize them with negative infinity to ensure that our algorithm will work correctly even if the input data contains values smaller than any possible input we expect to encounter.

Line 14–28: We use a loop to traverse the whole array. In this loop, we replace secondLargest with largest and largest with num if num is greater than largest. Also, we replace secondLargest with num if num is greater than secondLargest and less than the largest variable. After traversing the whole array, we'll have the second largest value of the array in the secondLargest variable.

Line 34–37: We initialize an array of numbers. We then call the findSecondLargest function and print the returned value.

The time complexity of this approach is O(n)O(n) because it iterates over the elements of the array $arr once.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved