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.
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
<?phpfunction findSecondLargest(array $arr){//sort the array in ascending ordersort($arr);//save the element from the second last position of sorted array$secondLargest = $arr[sizeof($arr)-2];//return second-largest numberreturn $secondLargest;}//create an array of numbers$arr = array(7,2,8,14,6,3,18,11,4,5);//call the function and print outputecho "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 sort() function in PHP takes $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
<?phpfunction findSecondLargest(array $arr) {//If array is empty then returnif(empty($arr)) {return;}//Initialize largest and second largest with negative values$largest = PHP_INT_MIN;$secondLargest = PHP_INT_MIN;//Traverse the whole arrayforeach($arr as $num) {//If element is greater than the value of current largestif($num > $largest) {//update largest and second largest$secondLargest = $largest;$largest = $num;}//If element is greater than secondLargest and less than largestif($num > $secondLargest && $num < $largest) {//update second largest$secondLargest = $num;}}//return second largest value of arrayreturn $secondLargest;}//create an array of numbers$arr = array(7,2,8,14,6,3,18,11,4,5);//call the function and print outputecho "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 $arr once.
Free Resources