# C++ Program to find the second largest element from the array

The purpose of an array is to store a similar type of data in a series of the memory places that can be accessed using base addresses and indexes. We use arrays to hold data for a variety of purposes in many different applications. Finding minimum and maximum elements are a fairly common example of arrays that are needed in several applications including sorting, etc. In this article, we will see how we can find the second-largest element from an array in C++.

## Understanding the concept with examples

Given array A = [89, 12, 32, 74, 14, 69, 45, 12, 99, 85, 63, 32]
The second largest element is 89


In the above example, there are 12 elements present in the array. The largest element is 99 in the array and the second largest element is 89. To find the second-largest element in the first approach, we just sort the elements in either ascending or descending order, then return the second last or the second element directly to get the second-largest element. The algorithm is like below −

### Algorithm

• Take the array A of size n

• sort the array A based on the non-increasing order of their values

• return A[ 1 ] // since 0th index is holding largest element

### Example

#include <iostream>
#include <algorithm>
# define Z 30

using namespace std;

void displayArr(int arr[], int n ) {
for( int i = 0; i < n; i++ ){
cout << arr[ i ] << ", ";
}
cout << endl;
}

int getSecondLargest( int A[], int n ){
sort( A, A + n, greater<int>() );
return A[ 1 ];
}

int main() {
int arr[ Z ] = {84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20};
int n = 15;

cout << "Given array elements: ";
displayArr( arr, n);

cout << "The second largest element: " << getSecondLargest( arr, n );
}


### Output

Given array elements: 84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20,
The second largest element: 94


## Using Double Traversal

The above method looks simple but this process is not efficient for this problem. Since we are using sorting, it will take at least O(n.log n) time to perform sorting. But we can solve this in linear time as well. In this current approach, we go through the array of elements twice and find the second largest element. Let's check the algorithm.

### Algorithm

• Take the array A of size n

• largest := -infinity

• secLargest := -infinity

• for each element e in A, do

• if e is larger than largest, then

• largest = e

• end if

• end for

• for each element e in A, do

• if e is larger than secLargest but smaller than largest, then

• secLargest = e

• end if

• end for

• return secLargest

### Example

#include <iostream>
#include <algorithm>
# define Z 30

using namespace std;

void displayArr(int arr[], int n ) {
for( int i = 0; i < n; i++ ){
cout << arr[ i ] << ", ";
}
cout << endl;
}

int getSecondLargest( int A[], int n ){
int largest = -99999;
for( int i = 0; i < n; i++ ) {
if( A[i] > largest ){
largest = A [ i ];
}
}
int secLargest = -99999;
for( int i = 0; i < n; i++ ) {
if( A[i] > secLargest && A[i] < largest ){
secLargest = A [ i ];
}
}
return secLargest;
}

int main() {
int arr[ Z ] = {84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20};
int n = 15;

cout << "Given array elements: ";
displayArr( arr, n);

cout << "The second largest element: " << getSecondLargest( arr, n );
}


### Output

Given array elements: 84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20,
The second largest element: 94


## Using Single Traversal

The above solution traverses through the array twice. In the first run, find the largest element from the array, then in the second run, search element which is the largest but not larger than the first largest. Since arrays are linear data structures and each traversal takes O(n) time, the ultimate time taken by the solution is O(2n) which is also linear, and similar to O(n). But this is not an efficient solution, we can solve this by a single traversal only. Let us see the algorithm for it.

### Algorithm

• Take the array A of size n

• largest := A[0]

• for starting index from 1 to n - 1, do

• if the current element A[ i ] is greater than largest, then

• secLargest := largest

• largest := A[ i ]

• otherwise when A[ i ] is in between largest and secLargest, then

• secLargest := A[ i ]

• end if

• end for

• return secLargest

### Example

#include <iostream>
#include <algorithm>
# define Z 30

using namespace std;

void displayArr(int arr[], int n ) {
for( int i = 0; i < n; i++ ){
cout << arr[ i ] << ", ";
}
cout << endl;
}

int getSecondLargest( int A[], int n ){
int largest = A[ 0 ];
int secLargest = -9999;
for( int i = 1; i < n; i++ ) {
if( A[i] > largest ){
secLargest = largest;
largest = A[ i ];
}
else if( secLargest < A[ i ] && A[ i ] != largest ) {
secLargest = A[ i ];
}
}
return secLargest;
}

int main() {
int arr[ Z ] = {84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20};
int n = 15;

cout << "Given array elements: ";
displayArr( arr, n);

cout << "The second largest element: " << getSecondLargest( arr, n );
}


### Output

Given array elements: 84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20,
The second largest element: 94


## Conclusion

In this article, we have seen three different approaches to finding the second-largest element from the given array. The first method is using sorting. However, this solution is not efficient and will take O(n log n ) time at a minimum. The latter solutions are efficient since they are taking linear time. The second solution is performing using double traversal on the array which can also be optimized by a single traversal as shown in the third solution.

Updated on: 13-Dec-2022

4K+ Views