Open In App

JavaScript Program to Find the Largest Three Elements in an Array

Last Updated : 18 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are given an array of numbers, we need to find the largest three elements in an array in JavaScript. We will explore the easiest and most efficient code of each approach and also go through the output of the code.

Approaches to find the largest three elements in an array in JavaScript

  • Using JavaScript sort() and slice() Method
  • Using JavaScript loops
  • Using JavaScript Math max() Method

Approach 1: Using JavaScript sort() Method

First, we will sort the array in descending order. We will use the sort() function to sort the array, once the sorting is completed, the largest elements will be at the start of the array. Using the slice() method, we will extract the first three elements which are the three largest elements in the array.

Example: This example demonstrated finding the largest three elements in an array by sorting the array and then picking the first three largest elements in JavaScript.

Javascript




// Sort the array and then pick the 
// first three largest elements
function findLargestThreeElementsUsingSort(arr) {
    const sortedArrOutput = arr.sort((a, b) => b - a);
    const [firstLargestEle, secondLargestEle, 
            thirdLargestEle] = sortedArrOutput.slice(0, 3);
  
    return {
        "First Largest Element in Array": firstLargestEle,
        "Second Largest Element in Array": secondLargestEle,
        "Third Largest Element in Array": thirdLargestEle,
    };
}
  
const inputArray = [12, 56, 7, 89, 43, 21];
const outputElements = 
    findLargestThreeElementsUsingSort(inputArray);
console.log(outputElements);


Output

{
  'First Largest Element in Array': 89,
  'Second Largest Element in Array': 56,
  'Third Largest Element in Array': 43
}

Approach 2: Using JavaScript loops

Here, we will use the JavaScript loop to iterate through the array of elements once. We will keep three variables as (‘firstLargestEle’, ‘secondLargestEle’, and ‘thirdLargestEle’. Firstly, we will initialize the ‘firstLargestEle‘ to the first element of the input array and ‘secondLargestEle‘, and ‘thirdLargestEle‘ to the negative infinity which is used to handle the negative numbers. Later, we will apply the comparision logic in the loop and return the three largest elements from the array.

Example: This example demonstrated finding the largest three elements in an array by using loops to find the three largest elements in JavaScript.

Javascript




//Using Loops 
function largestThreeElements(arr) {
    let firstLargestEle = arr[0];
    let secondLargestEle = -Infinity;
    let thirdLargestEle = -Infinity;
  
    for (const num of arr) {
        if (num > firstLargestEle) {
            thirdLargestEle = secondLargestEle;
            secondLargestEle = firstLargestEle;
            firstLargestEle = num;
        } else if (num > secondLargestEle) {
            thirdLargestEle = secondLargestEle;
            secondLargestEle = num;
        } else if (num > thirdLargestEle) {
            thirdLargestEle = num;
        }
    }
  
    return {
        "First Largest Element in Array": firstLargestEle,
        "Second Largest Element in Array": secondLargestEle,
        "Third Largest Element in Array": thirdLargestEle,
    };
}
  
const inputArray = [12, 56, 7, 89, 43, 21];
const outputElements =
    largestThreeElements(inputArray);
console.log(outputElements);


Output

{
  'First Largest Element in Array': 89,
  'Second Largest Element in Array': 56,
  'Third Largest Element in Array': 43
}

Approach 3: Using JavaScript Math.max() Method

Here, like in Approach 2, rather than having three variables, we will use the Math.max() function to iterate and find the three largest elements.

Example: This example demonstrated finding the three largest elements in an array by using the ‘Math.max’ function to find the three largest elements in JavaScript.

Javascript




//Using Math.max() function
function largestThreeElements(arr) {
  
    const firstLargestEle = Math.max(...arr);
  
    arr = arr.filter((num) => num !== firstLargestEle);
  
    const secondLargestEle = Math.max(...arr);
  
    arr = arr.filter((num) => num !== secondLargestEle);
  
    const thirdLargestEle = Math.max(...arr);
  
    return {
        "First Largest Element in Array": firstLargestEle,
        "Second Largest Element in Array": secondLargestEle,
        "Third Largest Element in Array": thirdLargestEle,
    };
}
  
const inputArray = [12, 56, 7, 89, 43, 21];
const outputElements =
    largestThreeElements(inputArray);
console.log(outputElements);


Output

{
  'First Largest Element in Array': 89,
  'Second Largest Element in Array': 56,
  'Third Largest Element in Array': 43
}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads