JavaScript Program for the Third Largest Element in an Array



For the third largest element in an array of distinct elements, we will discuss three different approaches. We will improve efficiency with each approach and discuss their complexities.

In this article we are having an array of distinct elements, our task is to find third largest element in an array of distinct elements in Javascript. Users must be familiar with array, loops and conditional statement.

Example

Input:
arr = [2,5,8,1,4,10,7]
=> Highest: 10, second highest: 8, third highest: 7

Output:
7

Approaches for Third Largest Element in Array

Here is a list of approaches for the third largest element in an array of distinct elements in Javascript which we will be discussing in this article with stepwise explanation and complete example codes.

Using Brute Force Approach

For the third largest element in an array of distinct elements, we have used brute force approach. It uses three loops to get the value of first, second and third largest element of the array making its complexity O(3n) simplified to O(n).

  • We have declared an array arr of distinct elements and defined a function thirdLargestEle() that accepts array arr as its argument.
  • In the function, first we check the length of the array using if statement. If it is less than 3, return a message indicating that the array should have at least 3 distinct elements.
  • Then we have declared three variables: first, second and third which will store the largest, second largest and third largest value of array respectively.
  • Then we have used three different for loops with if/else statement. The first for loop finds the largest element in array. Similarly, second and third for loop finds the second largest and third largest element in array.
  • The third variable is then returned, representing third largest element of the array.
  • Call the thirdLargestEle() function with the array as its argument. Print the result to the console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps for the third largest element in an array of distinct elements in Javascript using brute force approach.

let arr = [2, 5, 8, 1, 4, 10, 7];
function thirdLargestEle(arr) {
    if (arr.length  first) {
            first = num;
        }
    }
    for (let num of arr) {
        if (num > second && num  third && num 


Using Array Sorting

In this approach for the third largest element in an array of distinct elements, we have used sort() method of array in descending order and printed the third element of the sorted array.

  • We have defined an array arr of distinct elements and a function thirdLargestEle() that accepts array arr as its argument.
  • In the function, first we check the length of the array using if statement. If it is less than 3, return a message indicating that the array should have at least 3 distinct elements.
  • Sort the array in descending order using the sort() method and a comparison function.
  • Return the third element in the sorted array, which is the third largest element in the original array. We have used JSON.stringify() method to convert into string.
  • Call the thirdLargestEle() function with the array as its argument. Print the result to the console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps for the third largest element in an array of distinct elements in Javascript using array sorting.

let arr = [2, 5, 8, 1, 4, 10, 7];
function thirdLargestEle(arr) {
    if (arr.length 


Using Three Pointers

In this approach, we have used three pointers for the third largest element in an array of distinct elements. We have used first, second and third as pointers and keep updating these in each iteration.

  • We have declared an array arr of distinct elements and defined a function thirdLargestEle() that accepts array arr as its argument.
  • In the function, first we check the length of the array using if statement. If it is less than 3, return a message indicating that the array should have at least 3 distinct elements.
  • Then we have declared three variables: first, second and third which will store the largest, second largest and third largest value of array respectively.
  • Then we have used for...of loop and if/else statement to find the third largest element. The for..of iterates over each element of array and if condition checks if the element of array i.e num is greater than first.
  • If num is greater than first then second variable is assigned to third and first variable is asssigned to second and num gets assigned to first.
  • If num is greater than second then second value is assigned to third and num is assigned to second.
  • If num is greater than third then num is assigned to third.
  • Return the third element in the sorted array, which is the third largest element in the original array. We have used JSON.stringify() method to convert into string.
  • Call the thirdLargestEle() function with the array as its argument. Print the result to the console using console.log() method.

Solution

arr = [2,5,8,1,4,10,7]
Initially: first = second = third = -Infinity

After first iteration (num=2): num > first
first = 2, second = third = -Infinity

After second iteration (num=5): num > first
first = 5, second = 2, third = -Infinity

After third iteration (num=8): num > first
first = 8, second = 5, third = 2

After fourth iteration (num=1): num  No change

After fifth iteration (num=4): num > third
first = 8, second = 5, third = 4

After sixth iteration (num=10): num > first
first = 10, second = 8, third = 5

After seventh iteration (num=7): num > third
first = 10, second = 8, third = 7

Example

Here is a complete example code implementing above mentioned steps for the third largest element in an array of distinct elements in Javascript using three pointers approach.

let arr = [2, 5, 8, 1, 4, 10, 7];
function thirdLargestEle(arr) {
    if (arr.length  first) {
            third = second;
            second = first;
            first = num;
        } else if (num > second) {
            third = second;
            second = num;
        } else if (num > third) {
            third = num;
        }
    }
    return third;
}
const num = thirdLargestEle(arr)
console.log("The third largest element is:", num);

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Brute Force O(n) O(1)
Array Sorting O(n*log n) O(1)
Three Pointers O(n) O(1)

Conclusion

In this article, for the third largest element in an array of distinct elements in Javascript we have discussed three different approaches. The approaches are: using brute force method, by array sorting and three pointers technique. Out of all three approaches, three pointers approach is most efficient one.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2024-12-06T15:47:04+05:30

715 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements