Files
30-seconds-of-code/snippets/minN.md
Rohit Tanwar 511dc79d65 Update minN.md
2018-01-03 11:40:39 +05:30

17 lines
521 B
Markdown

### minN
Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
Sort's the array's shallow copy in ascending order and returns the first n elements
Skip the second argument to get a single element(in the form of a array)
``` js
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
```
``` js
minN([1,2,3]); // [1]
minN([1,2,3],2); // [1,2]
minN([1,2,3],4); // [1,2,3]
```