Skip to content

Latest commit

 

History

History
23 lines (19 loc) · 649 Bytes

removing_elements.md

File metadata and controls

23 lines (19 loc) · 649 Bytes

Description

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!

My Solution

def remove_every_other(arr)
  arr.select.with_index { |_, i| i.even? }
end

Better/Alternative solution from Codewars

def remove_every_other(arr)
  arr.each_slice(2).map(&:first)
end