Given an array of non-negative integers, your task is to only keep certain elements of it, as described below.
Let's say the array is
[1, 3, 2, 4, 11, 5, 2, 0, 13, 10, 1]
.First get the first element of the array,
n
. Keep the firstn
elements and discard the next one (discard then+1
th). The new array is[1, 2, 4, 11, 5, 2, 0, 13, 10, 1]
.Then, you grab the element following the one removed and do the exact same thing. Reapplying the process, we get
[1, 2, 11, 5, 2, 0, 13, 10, 1]
You repeat the process until you arrive outside the array's bounds / there are no elements left in the array. We stop because
11
is higher than the length of the array.Now you should output the result.
Input / output may be taken / provided in any standard form. The array will never be empty, and will only contain non-negative integers. All standard loopholes are forbidden.
This is code-golf so the shortest code in bytes wins!
Test Cases
Input --> Output [1, 2, 3, 4, 5] --> [1, 3, 4] [6, 1, 0, 5, 6] --> [6, 1, 0, 5, 6] [1, 3, 2, 4, 11, 5, 2, 0, 13, 10, 1] --> [1, 2, 11, 5, 2, 0, 13, 10, 1] [2, 2, 2, 2, 2, 2] --> [2, 2] [1, 2, 3, 1, 2, 3, 1, 2, 3] -> [1, 2] [3, 1, 2, 4, 0] --> []*
* The last test case involves 0
, so I decided to post the process such that it is clearer:
[3, 1, 2, 4, 0] --> [3, 1, 2, 0] --> [1, 2, 0] --> [1, 0] --> [0] --> [] )
2
removed in the first step instead of3
? \$\endgroup\$ – Leaky Nun Aug 21 '17 at 19:09[1, 2, 3, 1, 2, 3, 1, 2, 3]
\$\endgroup\$ – Rod Aug 21 '17 at 19:55n
", you always start from the beginning of the array to keepn
elements? Not (as I thought at first glance) keepn
elements where the first element is then
you are evaluating? \$\endgroup\$ – Brian J Aug 22 '17 at 13:10