How can I delete one element from an array by value How can I delete one element from an array by value arrays arrays

How can I delete one element from an array by value


I think I've figured it out:

a = [3, 2, 4, 6, 3, 8]a.delete(3)#=> 3a#=> [2, 4, 6, 8]


Borrowing from Travis in the comments, this is a better answer:

I personally like [1, 2, 7, 4, 5] - [7] which results in => [1, 2, 4, 5] from irb

I modified his answer seeing that 3 was the third element in his example array. This could lead to some confusion for those who don't realize that 3 is in position 2 in the array.


Another option:

a = [2,4,6,3,8]a -= [3]

which results in

=> [2, 4, 6, 8]