6 kyu
N smallest elements in original order
937 of 3,004GiacomoSorbi
Description:
Your task is to write a function that does just what the title suggests (so, fair warning, be aware that you are not getting out of it just throwing a lame basic sorting method there) with a list of integers and the expected number n
of smallest elements to return.
Also:
- the number of elements to be returned cannot be higher than the list length;
- elements can be duplicated;
- in case of duplicates, just return them according to the original order (see third example for more clarity).
Same examples and more in the test cases:
Array | N | Expected |
---|---|---|
[1, 2, 3, 4, 5] |
3 |
[1, 2, 3] |
[5, 4, 3, 2, 1] |
3 |
[3, 2, 1] |
[1, 2, 3, 4, 1] |
3 |
[1, 2, 1] |
[1, 2, 3, -4, 0] |
3 |
[1, -4, 0] |
[1, 2, 3, 4, 5] |
0 |
[] |
Performance version by FArekkusu also available.
Arrays
Lists
Data Structures
Algorithms
Similar Kata:
Stats:
Created | May 4, 2018 |
Published | May 5, 2018 |
Warriors Trained | 6407 |
Total Skips | 180 |
Total Code Submissions | 23337 |
Total Times Completed | 3004 |
JavaScript Completions | 937 |
Ruby Completions | 109 |
Crystal Completions | 6 |
Python Completions | 1338 |
C++ Completions | 238 |
C Completions | 112 |
NASM Completions | 3 |
Haskell Completions | 57 |
C# Completions | 180 |
Rust Completions | 146 |
Julia Completions | 4 |
Total Stars | 142 |
% of votes with a positive feedback rating | 93% of 553 |
Total "Very Satisfied" Votes | 495 |
Total "Somewhat Satisfied" Votes | 44 |
Total "Not Satisfied" Votes | 14 |
Total Rank Assessments | 6 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |