1.
Introduction
2.
Brute-Force Approach
2.1.
PseudoCode
2.2.
Implementation in Javascript
2.2.1.
Complexity Analysis
3.
Optimised Approach - 1
3.1.
PseudoCode
3.2.
Implementation in Javascript
3.2.1.
Complexity Analysis
4.
Optimised Approach - 2
4.1.
PseudoCode
4.2.
Implementation in Javascript
4.2.1.
Complexity Analysis
5.
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Find the Second Highest Element in an Array

Urwashi Priya
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

## Introduction

In this blog, we will discuss how to Find the second highest element in an array.

We are given N numbers in an array and we need to select the element which is the second largest in a group of numbers.

``````Example: Say 5 elements in an array given are: 0, 3, 2, 1, 4
Second largest number would be: 3``````

## Brute-Force Approach

The simple approach to Find the second highest element in an array is given below.

We just need to sort the given array in descending order and then extract the second element from the array

### PseudoCode

``````___________________________________________________________________
Procedure printsecondlargest(arr, arr_size) :
___________________________________________________________________
1.   let i, first, second
2.   if (arr_size < 2):
document.write(" Invalid Input ")
return
3.   arr.sort()
4.   for (i = arr_size - 2 till 0):
if (arr[i] != arr[arr_size - 1]):
document.write("The second largest element is " + arr[I]);
Return
document.write("There is no second largest element<br>")
end procedure
___________________________________________________________________``````

### Implementation in Javascript

``````function printsecondlargest(arr, arr_size) {
let i, first, second;

// minimum two elements must be present
if (arr_size < 2) {
document.write(" Invalid Input ");
return;
}

// sorting in ascending order
arr.sort();

// starting from back
for (i = arr_size - 2; i >= 0; i--) {
// if the element is not the same as the largest element
if (arr[i] != arr[arr_size - 1]) {
document.write("The second largest element is " + arr[i]);
return;
}
}

document.write("There is no second largest element<br>");
}

let arr= [0,3, 2, 1, 4 ];
let n = arr.length;
printsecondlargest(arr, n);``````

Output:

``````Sample Input:
0, 3, 2, 1, 4

Sample Output:
3``````

#### Complexity Analysis

Time Complexity: O(n * log n), time taken to sort.

Space Complexity: O(1), as no extra space is used.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Optimised Approach - 1

This problem can also be solved without using sort functions.

We need to traverse the array twice. In traversing the array the first time, the maximum element is taken out and then in the second traversal, we ignore the largest element found earlier, and extract the new largest element. Before this, first, initialise the first largest and the second largest number to a very minimum number.

Till now, I assume you must have got the basic idea of what has been asked in the problem statement. i.e: how to Find the Second Highest Element in an Array

find the second highest element in an array

Please have a look at the algorithm, and then again, you must give it a try.

### PseudoCode

``````___________________________________________________________________
procedure printsecondlargest(arr, arr_size) :
___________________________________________________________________
1.   let i; let largest = second = -2454635434;
2.   if (arr_size < 2):
document.write(" Invalid Input ")
return
3.   for (i = 0 to arr_size):
if (arr[i]>largest):
largest = arr[i]
4.   for (i = 0 to i<arr_size):
if (arr[i]>second && arr[i]<largest):
second = arr[i]
5.   if (second == -2454635434):
document.write("There is no second largest element<br>")
Else
document.write("The second largest element is " + second);
return
end procedure
___________________________________________________________________``````

### Implementation in Javascript

``````function printsecondlargest(arr, arr_size) {
let i;
let largest = second = -2454635434;

// minimum two elements must be present
if (arr_size < 2) {
document.write(" Invalid Input ");
return;
}

// extracting the first largest element
for (i = 0;i<arr_size;i++){
if (arr[i]>largest){
largest = arr[i];
}
}

// extracting the second largest element
for (i = 0 ;i<arr_size;i++){
if (arr[i]>second && arr[i]<largest){
second = arr[i];
}
}

if (second == -2454635434){

document.write("There is no second largest element<br>");
}
else{
document.write("The second largest element is " + second);
return;
}
}

let arr= [ 0, 3, 2, 1, 4 ];
let n = arr.length;
printsecondlargest(arr, n);``````

Output:

``````Sample Input:
0, 3, 2, 1, 4

Sample Output:
3``````

#### Complexity Analysis

Time Complexity: O(n).

Analysing Time Complexity:

Only array traversal is required.

Space complexity: O(1), no extra space is taken.

## Optimised Approach - 2

This problem can also be solved in a more optimised way. Here when we start traversing the array we put the first number into largest, if we find the next number to be larger than the first, then we put this number in the second and update the largest. This continues till we reach the end of the array.

Time complexity = O(n), as only one traversal is required.

Till now, I assume you must have got the basic idea of what has been asked in the problem statement. i.e: how to Find the Second Highest Element in an Array

find the second highest element in an array

Please have a look at the algorithm, and then again, you must give it a try.

### PseudoCode

``````procedure printsecondlargest(arr, arr_size) :
___________________________________________________________________
1.   let i; let largest = second = -2454635434;
2.   if (arr_size < 2):
document.write(" Invalid Input ")
return
3.   for (i = 0 to arr_size):
if (arr[i]>largest):
second=largest
largest = arr[I]
Else if(arr[i]!=largest && arr[i]>second ):
second = arr[i]
4.   if (second == -2454635434):
document.write("There is no second largest element<br>")
Else
document.write("The second largest element is " + second);
return
end procedure
___________________________________________________________________``````

### Implementation in Javascript

``````function printsecondlargest(arr, arr_size) {
let i;
let largest = second = -2454635434;

// minimum 2 elements must be present
if (arr_size < 2) {
document.write(" Invalid Input ");
return;
}

// finding the largest element

for (i = 0 ;i<arr_size;i++){
if (arr[i]>largest){
second = largest ;
largest = arr[i]
}

else if (arr[i]!=largest && arr[i]>second ){
second = arr[i];
}
}

if (second == -2454635434){
document.write("There is no second largest element<br>");
}
else{
document.write("The second largest element is " + second);
return;
}
}
let arr= [ 0, 3, 2, 1, 4 ];
let n = arr.length;
printsecondlargest(arr, n);``````

Output:

``````Sample Input:
0, 3, 2, 1, 4

Sample Output:
3``````

#### Complexity Analysis

Time Complexity: O(n).

Analysing Time Complexity:

Only array traversal is required.

Space complexity: O(1), no extra space is taken.

Also check out - Inorder Predecessor  and Fibonacci Series in JavaScript

1. Which sorting algorithm does the javascript sort function use?
The sorting function varies from engine to engine. Mostly, it uses quicksort or insertion sort.

2. Is javascript sort fast?
Yes, the javascript sort function is faster. It takes the most suitable to sort the array.

3. Why is a script tag required in javascript?
It is used to embed the client-side code of javascript. Generally, it differentiates the code from HTML and CSS.

## Conclusion

This article taught us how to Find the Second Highest Element in an Array. We discussed its implementation using illustrations of three different methods, their pseudocodes, and then proper codes.

We hope you could take away critical techniques like analyzing problems by walking over the execution of the examples and finding out the recursive pattern followed.

Now, we recommend you practice problem sets based on sorting to master your fundamentals. You can get a wide range of questions similar to this on Coding Ninjas Studio

Recommended problems -

It's not the end. Must solve the problem of similar types.

Happy Coding.

Live masterclass