I have an array. For example,
x = [1,2,3,4,5]
I know the command
x.delete_at(i)
will delete the element at index i from the array. But from what I've read, it can only handle one argument.
Let's say I have a variable that stores the indexes I wish to remove from x. For example,
y = [0,2,3]
My question: Is it possible to remove multiple elements from an array using another array that stores in the indexes you wish to delete at?
In essence, something like
x.delete_at(y)
Thanks! :)
Approach 1: Store the index of array elements into another array which need to be removed. Start a loop and run it to the number of elements in the array. Use splice() method to remove the element at a particular index.
You can use reject
with with_index
:
x.reject.with_index { |e, i| y.include? i } #=> [2, 5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With