← Back to challenges

Largest Numbers

JavaScriptHardarraysnumbersconditionsalgorithms

Instructions

Create a function that takes two arguments of an array of numbers arr and a constant number n and returns the n largest numbers from the given array.

Examples

largestNumbers(2, [4, 3, 2, 1]) ➞ [3, 4]

largestNumbers(1, [7, 19, 4, 2]) ➞ [19]

largestNumbers(3, [14, 12, 57, 11, 18, 16]) ➞ [16, 18, 57]

largestNumbers(0, [1, 3, 4, 2]) ➞ []

Notes

The returned array must be sorted in ascending order.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Age Difference Between Spouses