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

Stats:

CreatedMay 4, 2018
PublishedMay 5, 2018
Warriors Trained6407
Total Skips180
Total Code Submissions23337
Total Times Completed3004
JavaScript Completions937
Ruby Completions109
Crystal Completions6
Python Completions1338
C++ Completions238
C Completions112
NASM Completions3
Haskell Completions57
C# Completions180
Rust Completions146
Julia Completions4
Total Stars142
% of votes with a positive feedback rating93% of 553
Total "Very Satisfied" Votes495
Total "Somewhat Satisfied" Votes44
Total "Not Satisfied" Votes14
Total Rank Assessments6
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • GiacomoSorbi Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • KataSideKick Avatar
  • FArekkusu Avatar
  • monadius Avatar
  • hobovsky Avatar
  • cliffstamp Avatar
  • Minoru-kun Avatar
  • stellartux Avatar
  • trashy_incel Avatar
  • ejini战神 Avatar
  • saudiGuy Avatar
Ad