code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

047 Extract Each Kth

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

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

Example

For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be extractEachKth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10].
*/

function extractEachKth(array, k) {
  return array.filter((value, index) => (index + 1) % k !== 0)
}

test('extract Each Kth', () => {
  expect(extractEachKth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)).toEqual([1, 2, 4, 5, 7, 8, 10])
})

Created 2019-12-14T23:11:16+05:18 · Edit