Open In App

JavaScript Program to find the Nth smallest/largest element from an unsorted Array

Last Updated : 11 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 represents the value at the Nth position when considering all unique values in the array.

Examples:

Input:  
arr[]={1, 3, 2, 4, 8, 7, 5, 6}, 
N=3
Output:
largest=6,
smallest=3

There are several methods that can be used to find the Nth smallest/largest element from an unsorted Array in JavaScript, which are listed below:

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using Sorting() Method

In this approach, we sort the given array in descending or ascending order and return the element at the N-1 index.

Follow the given steps to solve the problem (Nth Smallest):

  • Sort the input array in ascending order.
  • Return the element at the N-1 index in the sorted array(0 – Based indexing).

Follow the given steps to solve the problem (Nth Largest):

  • Sort the input array in the descending order.
  • Return the element at the N-1 index in the sorted array (0 – Based indexing).

Syntax:

array.sort();

Example: Below is the implementation of the above approach.

Javascript




let arr = [1, 3, 2, 4, 8, 7, 5, 6];
let n = 3;
  
let largest = (arr, n) => {
    arr.sort((a, b) =>
        b - a); return arr[n - 1]
};
let smallest = (arr, n) => {
    arr.sort((a, b) =>
        a - b); return arr[n - 1]
};
  
console.log("N'th Largest  ", largest(arr, n));
console.log("N'th Smallest ", smallest(arr, n));


Output

N'th Largest   6
N'th Smallest  3

Approach 2: Using Set() Method

Set data structure is used to find the Nth smallest as well as largest element, it stores the distinct elements in the sorted order.

Follow the given steps to solve the problem:

  • Insert all the elements of an array into the set.
  • we are using Sets (NthSmallestSet and NthLargestSet) to remove duplicate values from the array.
  • Sort the set accordingly in ascending or descending order.
  • Move index iterator to reach Nth element in the set
  • Return the value of the element at which the index iterator is pointing.

Note: Set can only be used when elements are distinct.

Syntax:

new Set([it]);

Example: Below is the implementation of the above approach.

Javascript




let arr = [1, 3, 2, 4, 8, 7, 5, 6];
let Nth_smallest = 3;
  
let NthSmallest = new Set([...arr]);
NthSmallest = [...NthSmallest].sort((a, b) => a - b);
  
let NthLargest = new Set([...arr]);
NthLargest = [...NthLargest].sort((a, b) => b - a);
  
for (const index of NthSmallest.values()) {
    if (Nth_smallest === 1) {
        console.log("N'th Smallest : ", index);
        break;
    }
    Nth_smallest = Nth_smallest - 1;
}
let Nth_largest = 3;
for (const index of NthLargest.values()) {
    if (Nth_largest === 1) {
        console.log("N'th Largest : ", index);
        break;
    }
    Nth_largest = Nth_largest - 1;
}


Output

N'th Smallest :  3
N'th Largest :  6


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads