Find the Second Largest Element in an Array in PHP
Last Updated :
18 Jun, 2024
We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further operations. These problems are useful in data analysis and for algorithm design.
These types of problem in PHP can be solved using the following three approaches:
Using Sorting method
Here, we are going to Sort the array ($arr) in descending order and then return the element at index 1, which will be the second largest element.
Example: This example shows the use of sorting method to find the second largest element.
PHP
<?php
function findSecondLargest($arr)
{
$size = count($arr);
if ($size < 2) {
return "Array should have at least two elements.";
}
rsort($arr);
return $arr[1];
}
$numbers = [10, 5, 8, 12, 6, 3, 9];
$secondLargest = findSecondLargest($numbers);
echo "Original array: " . implode(", ", $numbers) . "\n";
echo "Second largest element: " . $secondLargest;
OutputOriginal array: 10, 5, 8, 12, 6, 3, 9
Second largest element: 10
Using Two Variables Approach
We are going to define a function namely findSecondLargest() that takes an array $arr as input. We initialize two variables, $largest and $secondLargest, with the first element of the array. Then, we will iterate over each element $num in the array using a foreach loop. Inside the loop, we compare $num with $largest and when the loop ends, $secondLargest will hold the second largest element in the array, which is then returned by the function.
Example: This example shows the use of Two Variables to find the second largest element.
PHP
<?php
function findSecondLargest($arr)
{
$largest = $arr[0];
$secondLargest = $arr[0];
foreach ($arr as $num) {
if ($num > $largest) {
$secondLargest = $largest;
$largest = $num;
} elseif ($num > $secondLargest && $num != $largest) {
$secondLargest = $num;
}
}
return $secondLargest;
}
$numbers = [10, 5, 8, 20, 12, 20, 15];
$secondLargest = findSecondLargest($numbers);
echo "The second largest element is: " . $secondLargest;
?>
OutputThe second largest element is: 15
Remove Largest Element Approach
We are going to use that function which first finds the largest element in the array. It then removes it from the array using PHP array_diff(). In the remaining array, the function finds the largest element again, which is 18. Then, the second largest element in the given array is printed as the output.
Example: This example shows the use of removal of largest element to find the second largest element.
PHP
<?php
function findSecondLargest($arr)
{
$n = count($arr);
$largest = $arr[0];
for ($i = 1; $i < $n; $i++) {
if ($arr[$i] > $largest) {
$largest = $arr[$i];
}
}
$arr = array_diff($arr, [$largest]);
$secondLargest = $arr[0];
foreach ($arr as $num) {
if ($num > $secondLargest) {
$secondLargest = $num;
}
}
return $secondLargest;
}
$numbers = [10, 5, 8, 20, 15, 3, 12, 9, 7, 2, 18, 6, 11, 4, 14];
$secondLargest = findSecondLargest($numbers);
echo "The second largest element is: " . $secondLargest;
?>
OutputThe second largest element is: 18
Similar Reads
PHP Second most frequent element in an array Given an array we have to find the second most frequent element present in it. Examples: Input : array(3, 3, 4, 5, 5, 5, 9, 8, 8, 8, 8, 8); Output : Second most frequent element is: 5 Input : array("geeks", "for", "geeks"); Output : Second most frequent element is: for Here are some common approache
4 min read
Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
2 min read
JavaScript Program to Find kth Largest/Smallest Element in an Array JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are
5 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', '
2 min read