JavaScript program for the third largest element in an array of distinct elements


In this tutorial, we will explore different ways to solve the problem using JavaScript. We will discuss various strategies and approaches that can be used to find the third largest element, and we will provide step-by-step explanations for each method. By the end of this tutorial, readers should have a solid understanding of how to approach this problem and find the third largest element in an array of distinct elements using JavaScript.

Before we start, let's first understand what an array is. An array is a group of elements, and each element in the array is assigned a unique index. The index starts from 0, and the last element in the array is assigned an index of n-1, where n is the size of the array.

Problem Statement

If an array of distinct integers with n elements is given, the task is to determine the third largest element present in the array.

For Example

Input: arr1 = [3, 7, 1, 9, 5];
Output: 5

Explanation − The third largest element of [3, 7, 1, 9, 5] is 5, so the program correctly outputs 5.

Input: arr2 = [1, 2];
Output: Array should have at least 3 distinct elements

Explanation − The input array [1, 2] has less than 3 elements, so the program correctly outputs the message Array doesn't have the third largest element.

Input: arr3 = [10, 4, 8, 6, 2, 9];
Output: 6

Explanation − The third largest element of [10, 4, 8, 6, 2, 9] is 6, so the program outputs 6.

Algorithm

STEP 1 − Define an array of distinct elements.

STEP 2 − Define a function called thirdLargestElement that takes an array as its argument.

STEP 3 − Check if the length of the array is less than 3. If it is, return a message indicating that the array should have at least 3 distinct elements.

STEP 4 − Sort the array in descending order using the sort() method and a comparison function.

STEP 5 − Return the third element in the sorted array, which is the third largest element in the original array.

STEP 6 − Call the thirdLargestElement() function with the array as its argument.

STEP 7 − Print the result to the console using console.log() method.

Now, it's time to implement this algorithm using Javascript. So let’s execute this algorithm with the help of an example where we implement this algorithm with the help of JavaScript.

Example

In the below JavaScript program, we find the third largest element in an array of distinct elements by first sorting the array in descending order and then returning the third element in the sorted array. If the array does not contain at least 3 distinct elements, a message is returned indicating that the array should have at least 3 elements.

let numbers = [2, 5, 8, 1, 4, 10, 7];
function thirdLargestElement(arr) {
   // Check if array contains at least 3 elements
   if (arr.length < 3) {
      return "Array should have at least 3 distinct elements";
   }
   // Sort the array in descending order
   arr.sort(function(a, b){return b-a});
   // Return the third largest element
   return arr[2];
}
console.log("Array:", JSON.stringify(numbers))
const num = thirdLargestElement(numbers)
console.log("The third largest element is:", num);

Conclusion

So, in this tutorial, we learn about finding the third largest element in an array of distinct elements using JavaScript, which is a simple and straightforward process. The algorithm involves creating an array of distinct elements, sorting the array in descending order, retrieving the third element in the array, and printing it as the third largest element.

Updated on: 17-Apr-2023

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements