알고리즘 54 - Removing Elements

tamagoyakii·2021년 10월 18일
0

알고리즘

목록 보기
54/89

Q.

Take an array and remove every second element from the array. Always keep the first element and start removing with the next element.

Example:

["Keep", "Remove", "Keep", "Remove", "Keep", ...] --> ["Keep", "Keep", "Keep", ...]
None of the arrays will be empty, so you don't have to worry about that!

A)

function removeEveryOther(arr){
  let ret = [];
  for (el in arr) {
    if (el % 2 === 0)
      ret.push(arr[el]);
  }
  return ret;
}

0개의 댓글