Skip to content

ExtractEach Kth

Given array of integers, remove each kth element from it.

extracteach_kth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10]

Example

For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be

Solution

py
def extracteach_kth(input_list, k):
    return [input_list[i] for i in range(len(input_list)) if i % k != k - 1]


print(extracteach_kth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3))
js
function extractEachKth(input_list, k) {
  return input_list.filter((_, i) => (i + 1) % k !== 0);
}

console.log(extractEachKth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3));

my thoughts are neither my employer's nor my wife's