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
Comparableprotocol. - An integer
Nrepresenting the number of smallest elements to return.
Output​
- An array containing the N smallest elements from the collection, sorted in ascending order.
Constraints​
- If
Nis 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: 3Output:
[1, 2, 3]
Input:
["q", "f", "k"],count: 10Output:
["f", "k", "q"]
Input:
[256, 16],count: 3Output:
[16, 256]
Input:
[String](),count: 3Output:
[]