Given an array of integers, our task is to write a program that efficiently finds the second largest element present in the array.
Example:
Input: arr[] = {12, 35, 1, 10, 34, 1}
Output: The second largest element is 34.
Explanation: The largest element of the
array is 35 and the second
largest element is 34
Input: arr[] = {10, 5, 10}
Output: The second largest element is 5.
Explanation: The largest element of
the array is 10 and the second
largest element is 5
Input: arr[] = {10, 10, 10}
Output: The second largest does not exist.
Explanation: Largest element of the array
is 10 there is no second largest element
Simple Solution
Approach: The idea is to sort the array in descending order and then return the second element which is not equal to largest element from the sorted array.
C++14
// C++ program to find second largest // element in an array #include <bits/stdc++.h> using namespace std; /* Function to print the second largest elements */void print2largest(int arr[], int arr_size) { int i, first, second; /* There should be atleast two elements */ if (arr_size < 2) { printf(" Invalid Input "); return; } // sort the array sort(arr, arr + arr_size); // start from second last element // as the largest element is at last for (i = arr_size - 2; i >= 0; i--) { // if the element is not // equal to largest element if (arr[i] != arr[arr_size - 1]) { printf("The second largest element is %d\n", arr[i]); return; } } printf("There is no second largest element\n"); } /* Driver program to test above function */int main() { int arr[] = { 12, 35, 1, 10, 34, 1 }; int n = sizeof(arr) / sizeof(arr[0]); print2largest(arr, n); return 0; } |
Output:
The second largest element is 34
Complexity Analysis:
- Time Complexity: O(n log n).
Time required to sort the array is O(n log n). - Auxiliary space: O(1).
As no extra space is required.
Better Solution:
Approach: The approach is to traverse the array twice. In the first traversal find the maximum element. In the second traversal find the greatest element less than the element obtained in first traversal.
C++14
// C++ program to find second largest // element in an array #include <bits/stdc++.h> using namespace std; /* Function to print the second largest elements */void print2largest(int arr[], int arr_size) { int i, first, second; /* There should be atleast two elements */ if (arr_size < 2) { printf(" Invalid Input "); return; } int largest = second = INT_MIN; // find the largest element for (int i = 0; i < arr_size; i++) { largest = max(largest, arr[i]); } // find the second largest element for (int i = 0; i < arr_size; i++) { if (arr[i] != largest) second = max(second, arr[i]); } if (second == INT_MIN) printf("There is no second largest element\n"); else printf("The second largest element is %d\n", second); } /* Driver program to test above function */int main() { int arr[] = { 12, 35, 1, 10, 34, 1 }; int n = sizeof(arr) / sizeof(arr[0]); print2largest(arr, n); return 0; } |
Java
// Java program to find second largest // element in an array class GFG{ // Function to print the second largest elements static void print2largest(int arr[], int arr_size) { int i, first, second; // There should be atleast two elements if (arr_size < 2) { System.out.printf(" Invalid Input "); return; } int largest = second = Integer.MIN_VALUE; // Find the largest element for(i = 0; i < arr_size; i++) { largest = Math.max(largest, arr[i]); } // Find the second largest element for(i = 0; i < arr_size; i++) { if (arr[i] != largest) second = Math.max(second, arr[i]); } if (second == Integer.MIN_VALUE) System.out.printf("There is no second " + "largest element\n"); else System.out.printf("The second largest " + "element is %d\n", second); } // Driver code public static void main(String[] args) { int arr[] = { 12, 35, 1, 10, 34, 1 }; int n = arr.length; print2largest(arr, n); } } // This code is contributed by Amit Katiyar |
C#
// C# program to find second largest // element in an array using System; class GFG{ // Function to print the second largest elements static void print2largest(int []arr, int arr_size) { // int first; int i, second; // There should be atleast two elements if (arr_size < 2) { Console.Write(" Invalid Input "); return; } int largest = second = int.MinValue; // Find the largest element for(i = 0; i < arr_size; i++) { largest = Math.Max(largest, arr[i]); } // Find the second largest element for(i = 0; i < arr_size; i++) { if (arr[i] != largest) second = Math.Max(second, arr[i]); } if (second == int.MinValue) Console.Write("There is no second " + "largest element\n"); else Console.Write("The second largest " + "element is {0}\n", second); } // Driver code public static void Main(String[] args) { int []arr = { 12, 35, 1, 10, 34, 1 }; int n = arr.Length; print2largest(arr, n); } } // This code is contributed by Amit Katiyar |
Output:
The second largest element is 34
Complexity Analysis:
- Time Complexity: O(n).
Two traversals of the array is needed. - Auxiliary space: O(1).
As no extra space is required.
Efficient Solution
Approach: Find the second largest element in a single traversal.
Below is the complete algorithm for doing this:
1) Initialize two variables first and second to INT_MIN as
first = second = INT_MIN
2) Start traversing the array,
a) If the current element in array say arr[i] is greater
than first. Then update first and second as,
second = first
first = arr[i]
b) If the current element is in between first and second,
then update second to store the value of current variable as
second = arr[i]
3) Return the value stored in second.
C++
// C++ program to find second largest // element in an array #include <bits/stdc++.h> using namespace std; // Function to print the second // largest elements void print2largest(int arr[], int arr_size) { int i, first, second; // There should be atleast two elements if (arr_size < 2) { cout << " Invalid Input "; return; } first = second = INT_MIN; for(i = 0; i < arr_size; i++) { // If current element is greater // than first then update both // first and second if (arr[i] > first) { second = first; first = arr[i]; } // If arr[i] is in between first // and second then update second else if (arr[i] > second && arr[i] != first) { second = arr[i]; } } if (second == INT_MIN) cout << "There is no second largest" "element\n"; else cout << "The second largest element is " << second; } // Driver code int main() { int arr[] = { 12, 35, 1, 10, 34, 1 }; int n = sizeof(arr) / sizeof(arr[0]); print2largest(arr, n); return 0; } // This code is contributed by shivanisinghss2110 |
C
// C program to find second largest // element in an array #include <limits.h> #include <stdio.h> /* Function to print the second largest elements */void print2largest(int arr[], int arr_size) { int i, first, second; /* There should be atleast two elements */ if (arr_size < 2) { printf(" Invalid Input "); return; } first = second = INT_MIN; for (i = 0; i < arr_size; i++) { /* If current element is greater than first then update both first and second */ if (arr[i] > first) { second = first; first = arr[i]; } /* If arr[i] is in between first and second then update second */ else if (arr[i] > second && arr[i] != first) second = arr[i]; } if (second == INT_MIN) printf("There is no second largest element\n"); else printf("The second largest element is %dn", second); } /* Driver program to test above function */int main() { int arr[] = { 12, 35, 1, 10, 34, 1 }; int n = sizeof(arr) / sizeof(arr[0]); print2largest(arr, n); return 0; } |
Java
// JAVA Code for Find Second largest // element in an array class GFG { /* Function to print the second largest elements */ public static void print2largest(int arr[], int arr_size) { int i, first, second; /* There should be atleast two elements */ if (arr_size < 2) { System.out.print(" Invalid Input "); return; } first = second = Integer.MIN_VALUE; for (i = 0; i < arr_size; i++) { /* If current element is smaller than first then update both first and second */ if (arr[i] > first) { second = first; first = arr[i]; } /* If arr[i] is in between first and second then update second */ else if (arr[i] > second && arr[i] != first) second = arr[i]; } if (second == Integer.MIN_VALUE) System.out.print("There is no second largest" + " element\n"); else System.out.print("The second largest element" + " is " + second); } /* Driver program to test above function */ public static void main(String[] args) { int arr[] = { 12, 35, 1, 10, 34, 1 }; int n = arr.length; print2largest(arr, n); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Python program to # find second largest # element in an array # Function to print the # second largest elements def print2largest(arr, arr_size): # There should be atleast # two elements if (arr_size < 2): print(" Invalid Input ") return first = second = -2147483648 for i in range(arr_size): # If current element is # smaller than first # then update both # first and second if (arr[i] > first): second = first first = arr[i] # If arr[i] is in # between first and # second then update second elif (arr[i] > second and arr[i] != first): second = arr[i] if (second == -2147483648): print("There is no second largest element") else: print("The second largest element is", second) # Driver program to test # above function arr = [12, 35, 1, 10, 34, 1] n = len(arr) print2largest(arr, n) # This code is contributed # by Anant Agarwal. |
C#
// C# Code for Find Second largest // element in an array using System; class GFG { // Function to print the // second largest elements public static void print2largest(int[] arr, int arr_size) { int i, first, second; // There should be atleast two elements if (arr_size < 2) { Console.WriteLine(" Invalid Input "); return; } first = second = int.MinValue; for (i = 0; i < arr_size; i++) { // If current element is smaller than // first then update both first and second if (arr[i] > first) { second = first; first = arr[i]; } // If arr[i] is in between first // and second then update second else if (arr[i] > second && arr[i] != first) second = arr[i]; } if (second == int.MinValue) Console.Write("There is no second largest" + " element\n"); else Console.Write("The second largest element" + " is " + second); } // Driver Code public static void Main(String[] args) { int[] arr = { 12, 35, 1, 10, 34, 1 }; int n = arr.Length; print2largest(arr, n); } } // This code is contributed by Parashar. |
PHP
<?php // PHP program to find second largest // element in an array // Function to print the // second largest elements function print2largest($arr, $arr_size) { // There should be atleast // two elements if ($arr_size < 2) { echo(" Invalid Input "); return; } $first = $second = PHP_INT_MIN; for ($i = 0; $i < $arr_size ; $i++) { // If current element is // smaller than first // then update both // first and second if ($arr[$i] > $first) { $second = $first; $first = $arr[$i]; } // If arr[i] is in // between first and // second then update // second else if ($arr[$i] > $second && $arr[$i] != $first) $second = $arr[$i]; } if ($second == PHP_INT_MIN) echo("There is no second largest element\n"); else echo("The second largest element is " . $second . "\n"); } // Driver Code $arr = array(12, 35, 1, 10, 34, 1); $n = sizeof($arr); print2largest($arr, $n); // This code is contributed by Ajit. ?> |
Output:
The second largest element is 34
Complexity Analysis:
- Time Complexity: O(n).
Only one traversal of the array is needed. - Auxiliary space: O(1).
As no extra space is required.
Related Article:
Smallest and second smallest element in an array
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find Second largest element in an array | Set 2
- Find just strictly greater element from first array for each element in second array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find last element after deleting every second element in array of n integers
- Largest element smaller than current element on left for every element in Array
- Find a pair in Array with second largest product
- Array formed from difference of each element from the largest element in the given array
- Find extra element in the second array
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count distinct elements after adding each element of First Array with Second Array
- Program to find largest element in an array
- Find largest element from array without using conditional operator
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Program to find largest element in an array using Dynamic Memory Allocation
- Replace elements with absolute difference of smallest element on left and largest element on right
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Number of ways to change the Array such that largest element is LCM of array
- Find the smallest and second smallest elements in an array

