Javascript Program for Third largest element in an array of distinct elements
Last Updated :
15 Sep, 2024
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:
- First, iterate through the array and find maximum.
- Store this as first maximum along with its index.
- Now traverse the whole array finding the second max, excluding the maximum element.
- Finally traverse the array the third time and find the third largest element i.e., excluding the maximum and second maximum.
JavaScript
// JavaScript program to find third
// Largest element in an array
// of distinct elements
function thirdLargest(arr, arr_size) {
/* There should be
atleast three elements */
if (arr_size < 3) {
console.log(" Invalid Input ");
return;
}
// Find first
// largest element
let first = arr[0];
for (let i = 1;
i < arr_size; i++)
if (arr[i] > first)
first = arr[i];
// Find second
// largest element
let second = Number.MIN_VALUE;
for (let i = 0;
i < arr_size; i++)
if (arr[i] > second &&
arr[i] < first)
second = arr[i];
// Find third
// largest element
let third = Number.MIN_VALUE;
for (let i = 0;
i < arr_size; i++)
if (arr[i] > third &&
arr[i] < second)
third = arr[i];
console.log("The third Largest " +
"element is ", third);
}
// Driver Code
let arr = [12, 13, 1,
10, 34, 16];
let n = arr.length;
thirdLargest(arr, n);
OutputThe 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:
- 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).
- Move along the input array from start to the end.
- 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.
- Else if the element is larger than the second, then update the value of second,and the second largest element becomes third largest.
- If the previous two conditions fail, but the element is larger than the third, then update the third.
- Print the value of third after traversing the array from start to end
JavaScript
// JavaScript program to find third
// Largest element in an array
function thirdLargest(arr, arr_size) {
/* There should be atleast three elements */
if (arr_size < 3) {
console.log(" Invalid Input ");
return;
}
// Initialize first, second and third Largest element
let first = arr[0], second = -1000000000, third = -1000000000;
// Traverse array elements to find the third Largest
for (let i = 1; i < arr_size; i++) {
/* If current element is greater than first,
then update first, second and third */
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second */
else if (arr[i] > second) {
third = second;
second = arr[i];
}
/* If arr[i] is in between second and third */
else if (arr[i] > third)
third = arr[i];
}
console.log("The third Largest element is " + third);
}
/* Driver program to test above function */
let arr = [12, 13, 1, 10, 34, 16];
let n = arr.length;
thirdLargest(arr, n);
OutputThe 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
OutputThe third Largest element is 13
Complexity Analysis:
- 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!
Similar Reads
Third largest element in an array of distinct elements Given an array of n integers, the task is to find the third largest element. All the elements in the array are distinct integers. Examples : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Input: arr[] = {
11 min read
JavaScript Program to Find Largest Element in an Array In this article, we are going to learn about the largest element in an array in JavaScript. The largest element in an array refers to the value that holds the greatest numerical or lexicographic (string) order among all elements present in the array. Example: Input : [10, 15, 38, 20, 13];Output: 38H
3 min read
JavaScript Program to Find kth Largest/Smallest Element in an Array JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are
5 min read
JavaScript Program to find the Nth smallest/largest element from an unsorted Array In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep
4 min read
How to get n largest elements from array in JavaScript ? Here in this article, we will see how we can find the n maximum element from the array using Javascript. Example: Input: arr = [1, 2, 3, 4, 5, 6], n = 3;Output: 4, 5, 6Explanation: Here we will see the 3 largest elements in the given array are 4, 5, 6. Input: arr = [5, 76, 32, 98, 52, 57] n = 2;Outp
3 min read
Javascript Program for Maximize elements using another array Given two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first array). The order of appeara
3 min read
Javascript Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21]Output :3976Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]Output :216556Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside
2 min read
Javascript Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note: k is always less than or equal to n.Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their appea
3 min read
JavaScript Program to Find Kth Smallest/Largest Element in a Sorted Matrix Given an n x n matrix, where every row and column is sorted in non-decreasing order. Find the kth smallest/largest element in the given 2D array. Examples: Input:k = 3 and array = 10, 20, 30, 40 15, 25, 35, 45 24, 29, 37, 48 32, 33, 39, 50 Output: 20 40Explanation: The 3rd smallest element is 20 The
3 min read
Longest Subarray consisting of unique elements from an Array Given an array arr[] consisting of N integers, the task is to find the largest subarray consisting of unique elements only. Examples: Input: arr[] = {1, 2, 3, 4, 5, 1, 2, 3} Output: 5 Explanation: One possible subarray is {1, 2, 3, 4, 5}. Input: arr[]={1, 2, 4, 4, 5, 6, 7, 8, 3, 4, 5, 3, 3, 4, 5, 6,
5 min read