# Find Second largest element in an array

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.

Input : arr[] = {10, 5, 10}
Output : The second largest element is 5.

Input : arr[] = {10, 10, 10}
Output : The second largest does not exist.
```

A simple solution will be first sort the array in descending order and then return the second element from the sorted array. The time complexity of this solution is O(nlogn).

A Better Solution 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. The time complexity of this solution is O(n).

A more Efficient Solution can be to 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 ` `#include   ` ` `  `/* 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 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_MIN) ` `        ``printf``(````"There is no second largest element "````); ` `    ``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 "````); ` `        ``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 "````); ` `        ``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

 ` ``\$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 "````); ` `    ``else` `        ``echo``(``"The second largest element is "` `. ``\$second` `. ````" "````); ` `} ` ` `  `// 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
```

Time complexity: O(n)
Auxiliary Space : O(1)

Related Article:
Smallest and second smallest element in an array

## tags:

Arrays Order-Statistics Arrays