Open In App

# Javascript Program for Third largest element in an array of distinct elements

Given an array of n integers, find the third largest element. All the elements in the array are distinct integers.
Example :

```Input: arr[] = {1, 14, 2, 16, 10, 20}
Output: The third Largest element is 14

Explanation: Largest element is 20, second largest element is 16
and third largest element is 14

Input: arr[] = {19, -10, 20, 14, 2, 16, 10}
Output: The third Largest element is 16

Explanation: Largest element is 20, second largest element is 19
and third largest element is 16```

Naive Approach: The task is to first find the largest element, followed by the second-largest element and then excluding them both find the third-largest element. The basic idea is to iterate the array twice and mark the maximum and second maximum element and then excluding them both find the third maximum element, i.e the maximum element excluding the maximum and second maximum.

• Algorithm:
1. First, iterate through the array and find maximum.
2. Store this as first maximum along with its index.
3. Now traverse the whole array finding the second max, excluding the maximum element.
4. Finally traverse the array the third time and find the third largest element i.e., excluding the maximum and second maximum.

## Javascript

 ``

• Output:

`The third Largest element is 13`
• Complexity Analysis:
• Time Complexity: O(n).
As the array is iterated thrice and is done in a constant time
• Space complexity: O(1).
No extra space is needed as the indices can be stored in constant space.

Efficient Approach: The problem deals with finding the third largest element in the array in a single traversal. The problem can be cracked by taking help of a similar problem- finding the second maximum element. So the idea is to traverse the array from start to end and to keep track of the three largest elements up to that index (stored in variables). So after traversing the whole array, the variables would have stored the indices (or value) of the three largest elements of the array.

• Algorithm:
1. Create three variables, first, second, third, to store indices of three largest elements of the array. (Initially all of them are initialized to a minimum value).
2. Move along the input array from start to the end.
3. For every index check if the element is larger than first or not. Update the value of first, if the element is larger, and assign the value of first to second and second to third. So the largest element gets updated and the elements previously stored as largest becomes second largest, and the second largest element becomes third largest.
4. Else if the element is larger than the second, then update the value of second,and the second largest element becomes third largest.
5. If the previous two conditions fail, but the element is larger than the third, then update the third.
6. Print the value of third after traversing the array from start to end

## Javascript

 ``

• Output:

`The third Largest element is 13`
• Complexity Analysis:
• Time Complexity: O(n).
As the array is iterated once and is done in a constant time
• Space complexity: O(1).
No extra space is needed as the indices can be stored in constant space.

Another Approach:

• Check if the length of the input array is less than 3, return “Invalid Input”.
• Initialize first, second, and third variables to be the smallest integer number possible using Number.MIN_SAFE_INTEGER constant.
• Traverse the input array and compare each element to first, second, and third to find the largest elements.
• If the element is greater than the first element, shift the values stored in second and third to the left and store the current element in the first position.
• Else if the element is greater than the second element, shift the value stored in third to the left and store the current element in the second position.
• Else if the element is greater than the third element, store the current element in the third position.
• After the traversal, return the value of the third variable, which contains the third largest element.

## Javascript

 `function` `thirdLargest(arr) {``  ``if` `(arr.length < 3) {``    ``return` `"Invalid Input"``;``  ``}` `  ``let [first, second, third] = Array(3).fill(Number.MIN_SAFE_INTEGER);` `  ``for` `(let num of arr) {``    ``if` `(num > first) {``      ``[third, second, first] = [second, first, num];``    ``} ``else` `if` `(num > second) {``      ``[third, second] = [second, num];``    ``} ``else` `if` `(num > third) {``      ``third = num;``    ``}``  ``}` `  ``return` `third;``}``// Example usage:``const arr = [12, 13, 1, 10, 34, 16];``console.log(``"The third Largest element is "``+ thirdLargest(arr)); ``// Output: 14`

Output

```The third Largest element is 13
```

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

Please refer complete article on Third largest element in an array of distinct elements for more details!