Skip to main content

Find N Smallest

Problem​

Write an extension for all collections that returns the N smallest elements as an array, sorted smallest first, where N is an integer parameter.

Input​

  • A collection (array) of elements that conform to the Comparable protocol.
  • An integer N representing the number of smallest elements to return.

Output​

  • An array containing the N smallest elements from the collection, sorted in ascending order.

Constraints​

  • If N is greater than the number of elements in the collection, return all elements sorted.
  • The collection can contain any type of elements that conform to Comparable.
  • If the collection is empty, return an empty array.

Example​

Input:
[1, 2, 3, 4], count: 3

Output:
[1, 2, 3]

Input:
["q", "f", "k"], count: 10

Output:
["f", "k", "q"]

Input:
[256, 16], count: 3

Output:
[16, 256]

Input:
[String](), count: 3

Output:
[]