C++ program to find second largest number in an array

In this blog, we will create a program to find the second largest number in an array in C++.

What is Array?

An array is a collection of similar data elements stored in contiguous memory locations. It is the simplest data structure where each data element can be directly accessed using only its index number.

Approach used to write the program

This program finds the second largest element in an array. The program prompts the user for n, which is the number of elements the user wishes to enter. The user is asked to enter the array elements. The program then finds the second largest element of the array using a for loop.

C++ program to find second largest element in an array

#include <iostream>
using namespace std;
int main(){
   int n, num[50], largest, second;
   cout<<"Enter number of elements: ";
   cin>>n;
   for(int i=0; i<n; i++){
      cout<<"Enter Array Element"<<(i+1)<<": ";
      cin>>num[i];
   }
   /* Here we are comparing first two elements of the
    * array, and storing the largest one in the variable
    * "largest" and the other one to "second" variable.
    */
   if(num[0]<num[1]){ 
      largest = num[1];
      second = num[0];
   }
   else{ 
      largest = num[0];
      second = num[1];
   }
   for (int i = 2; i< n ; i ++) {
      /* If the current array element is greater than largest
       * then the largest is copied to "second" and the element
       * is copied to the "largest" variable.
       */
      if (num[i] > largest) {
         second = largest;
         largest = num[i];
      }
      /* If current array element is less than largest but greater
       * then second largest ("second" variable) then copy the
       * element to "second"
       */
      else if (num[i] > second && num[i] != largest) {
         second = num[i];
      }
   }
   cout<<"Second Largest Element in array is: "<<second;
   return 0;
}

Output of the above code

This code will allow you to insert “n” number of inputs out of which the code will find the second largest element from the array.

Explanation of the code

The user enters 5 as the value of n, which means that the first for loop has run the times to store each element entered by the user in the array, the first element in num[0], the second in num[1], and so on.

After the first for loop is an if-else statement that compares the first two elements of the array and stores the larger one in the “largest” variable and the smaller one in the “second” variable. Note: In this program, the variable “largest” is for the largest element and “second” is for the second largest.

The second loop checks the remaining elements of the array and compares them one by one with the largest variable to see if there is any element larger than the largest, if there is, the element is copied to the largest.

In the second for loop, there is an else if part that checks to see if there is any element< greater than > than the second and then updates the second variable.