6 kyu

N smallest elements in original order (performance edition)

170 of 277FArekkusu

Description:

This challenge is based on a kata by GiacomoSorbi. Before doing this one it is advisable to complete the non-performance version first.


Task

You will be given an array of integers in a [1; 50] range, and a number n. You have to extract n smallest elements out of the array preserving their original order.

Notes

  • There will be duplicates in the array, and they have to be returned in the order of their each separate appearence.
  • This kata is an example of the "know your data" principle. Remember this while searching for the correct approach.

Examples

numbers = [1, 2, 3, 4, 5]
n = 3
result = [1, 2, 3]

numbers = [5, 4, 3, 2, 1]
n = 3
result = [3, 2, 1]

numbers = [1, 2, 4, 1, 2]
n = 3
result = [1, 2, 1]
Algorithms
Arrays
Performance

Stats:

CreatedMay 6, 2018
PublishedMay 6, 2018
Warriors Trained1455
Total Skips27
Total Code Submissions4503
Total Times Completed277
Python Completions170
JavaScript Completions94
Rust Completions35
Total Stars37
% of votes with a positive feedback rating81% of 93
Total "Very Satisfied" Votes69
Total "Somewhat Satisfied" Votes12
Total "Not Satisfied" Votes12
Total Rank Assessments4
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • FArekkusu Avatar
  • Voile Avatar
  • Minoru-kun Avatar
Ad