← Back to challenges

Nth Smallest Integer

JavaScriptHardarrayssortinglanguage_fundamentalslogic

Instructions

Given an unsorted array, create a function that returns the nth smallest integer (the smallest integer is the first smallest, the second smallest integer is the second smallest, etc).

Examples

nthSmallest([1, 3, 5, 7], 1) ➞ 1

nthSmallest([1, 3, 5, 7], 3) ➞ 5

nthSmallest([1, 3, 5, 7], 5) ➞ null

nthSmallest([7, 3, 5, 1], 2) ➞ 3

Notes

  • n will always be >= 1.
  • Each integer in the array will be distinct (there will be a clear ordering).
  • Given an out of bounds parameter (e.g. an array is of size k), and you are asked to find the m > k smallest integer, return null.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Strange Pair