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);

You can also try this code with Online Javascript Compiler
Run Code
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);

You can also try this code with Online Javascript Compiler
Run Code
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
Frequently Asked Questions
-
Which sorting algorithm does the javascript sort function use?
The sorting function varies from engine to engine. Mostly, it uses quicksort or insertion sort.
-
Is javascript sort fast?
Yes, the javascript sort function is faster. It takes the most suitable to sort the array.
-
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.