find Nth maximum number
in an array : duplicates allowed
[ sort ]

Find Nth maximum number in an array

Write a function that takes an array of numbers and a number as input and returns the Nth maximum (largest) number found in that array.

Duplicate values in the array are allowed. The original array is modified.


Example ...

Enter an array ...

[5, 3, 3, 8, 4, 6, 8, 7] array

3 find 3rd largest number in array

The function will return 7

Sort the array in descending order.

[8, 8, 7, 6, 5, 4, 3, 3] sorted array

8 1st largest number in array

8 2nd largest number in array

7 3rd largest number in array

6 4th largest number in array

5 5th largest number in array

4 6th largest number in array

3 7th largest number in array

3 8th largest number in array

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Find the Nth maximum number in an array using the in-built sort method.


sort() method sorts the elements of an array as strings.

By default, the elements of the array are converted to strings and sorted in ascending order by comparing their sequences based on their UTF-16 code unit values.

sort() method returns a reference to the original array, so mutating the returned array will mutate the original array as well.

sort() method is stable, it preserves the order of items in an array when their values are the same.

sort() method can sort an array of numbers, strings, or objects with custom functions.

syntax

array.sort()

By default the sort() method sorts the array in ascending order with the elements converted to strings.

This can lead to unexpected results when sorting arrays of numbers.

const arr2 = [2, 11, 1, 22, 33, 3];

arr2.sort(); returns ↴

[1, 11, 2, 22, 3, 33] NOT sorted accurately

To solve this limitation, a comparison callback function can be used to define the desired sorting order.

callback function a function passed into another function as an argument, which is then invoked within the outer function.

When sorting numbers, always use a compare function to ensure numerical comparison.


For sorting an array of numbers in ascending order, the comparison function should subtract the second number from the first number.

syntax

array.sort(compareFunction)

comparison function

(a, b) => a - b compares two elements ↴

a and b

a - b calculates the difference between the two numeric values ↴

If the result is ...

negativea sorted before b

positivea sorted after b

zero → order remains unchanged

const arr3 = [2, 11, 1, 22, 33, 3];

arr3.sort((a, b) => a - b); returns ↴

[1, 2, 3, 11, 22, 33] → ascending order

The comparison function can be changed to sort in descending order by swapping the elements for comparison to b - a

const arr4 = [2, 11, 1, 22, 33, 3];

arr4.sort((a, b) => b - a); returns ↴

[33, 22, 11, 3, 2, 1] → descending order


Initialize an array from which to find the Nth maximum number.

const array1 = [5, 3, 3, 8, 4, 6, 8, 7]; → user input

Initialize the Nth maximum number to find.

const num1 = 2; → user input


Define a function findNthMax to find the Nth maximum number.

function findNthMax(arr, num) {}

The function takes an array arr and a number num as input, and returns the Nth maximum number found. The original array is updated.

sort() method mutates the original array.

Sort the array in descending order (a, b) => b -a

const sortedArr = arr.sort((a, b) => b - a) sortedArr

The returned array will be sorted in descending order, with the highest number first and the lowest number last.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

num is the Nth maximum number to find.

Index of position 1 will be num - 1 1 - 1 [0]

Index of position 2 will be num - 1 2 - 1 [1]

Index of position 3 will be num - 1 3 - 1 [2]

and so on ...

Return the Nth maximum number found.

return sortedArr[num - 1]


Call the function with ↴

findNthMax(array1, num1);


Find the Nth maximum number in an array.

const array1 = [5, 3, 3, 8, 4, 6, 8, 7];

const num1 = 3;

function findNthMax(arr, num) {

const sortedArr = arr.sort((a, b) => b - a);

return sortedArr[num - 1];

}

call function

findNthMax(array1, num1); returns ↴

7 → 3rd largest number in array


Alternative - sort in ascending order and search from last element in array ↴

const array2 = [5, 3, 3, 8, 4, 6, 8, 7];

const num2 = 3;

function findNthMax2(arr, num) {

const sortedArr = arr.sort((a, b) => a - b);

return sortedArr[sortedArr.length - num];

}

call function

findNthMax2(array2, num2); returns ↴

7 → 3rd largest number in array


Alternative using slice() method to prevent mutating original array ↴

arr.sort((a, b) => b - a);

arr.slice().sort((a, b) => b - a);


Alternative using toSorted() method to prevent mutating original array ↴

arr.sort((a, b) => b - a);

arr.toSorted((a, b) => b - a);

Find Nth maximum number in an array

Enter Nth maximum number to find