In Ruby, How to Remove Only 1 Match in an Array Easily

In Ruby, is there a way to remove only 1 match in an Array easily?

>> a = [1,3,5,7,7]

>> a.slice!(a.index(7))
=> 7

>> a
=> [1,3,5,7]

Delete first instance of matching element from array

li.delete_at(li.index(n) || li.length)

li[li.length] is out of range, so the || li.length handles the case where n isn't in the list.

irb(main):001:0> li = [1,2,3,1,2,3]
=> [1, 2, 3, 1, 2, 3]
irb(main):002:0> li.delete_at(li.index(2) || li.length)
=> 2
irb(main):003:0> li.delete_at(li.index(42) || li.length)
=> nil
irb(main):004:0> li
=> [1, 3, 1, 2, 3]

How to remove only a single instance of a value from an array in Ruby?

You can use #index (or #find_index) to find the index of the first matching element, and #delete_at (or #slice!) to delete that element:

foo.delete_at(foo.index(4))

Here's another thread that discusses this issue. They recommend adding a guard in case the value being searched for doesn't appear in the array:

foo.delete_at(foo.index(4) || foo.length)

You could use a flag variable and #delete_if until the flag is flipped:

flag = false
foo.delete_if { |i| break if flag; !flag and i == 4 and flag = true }

You could partition the array into matches and non-matches, remove one of the matches, and reconcatenate the array:

matches, foo = foo.partition { |i| i == 4 }
foo.concat(matches[0..-2])

But I think the first option is best. :-)

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)
#=> 3
a
#=> [2, 4, 6, 8]

delete ONE array element by value in ruby

Pass the result of Array#find_index into Array#delete_at:

>> arr.delete_at(arr.find_index(3))

>> arr
=> [1, 1, 2, 2, 3, 4, 5]

find_index() will return the Array index of the first element that matches its argument. delete_at() deletes the element from an Array at the specified index.

To prevent delete_at() raising a TypeError if the index isn't found, you may use a && construct to assign the result of find_index() to a variable and use that variable in delete_at() if it isn't nil. The right side of && won't execute at all if the left side is false or nil.

>> (i = arr.find_index(3)) && arr.delete_at(i)
=> 3
>> (i = arr.find_index(6)) && arr.delete_at(i)
=> nil
>> arr
=> [1, 1, 2, 2, 3, 4, 5]

Ruby: What is the easiest way to remove the first element from an array?

"pop"ing the first element of an Array is called "shift" ("unshift"
being the operation of adding one element
in front of the array).

Remove matching string in array

I assume you wish to remove all elements of array that appear more than once.

array - array.each_with_object(Hash.new(0)) { |s,h| h[s] += 1 }.
select { |_,v| v > 1 }.
keys
#=> ["dog", "monkey", "bear"]

The steps are as follows.

g = array.each_with_object(Hash.new(0)) { |s,h| h[s] += 1 }
#=> {"dog"=>1, "cat"=>2, "monkey"=>1, "bear"=>1}
h = g.select { |_,v| v > 1 }
#=> {"cat"=>2}
a = h.keys
#=> ["cat"]
array - a
#=> ["dog", "monkey", "bear"]

How to remove all elements that satisfy a condition in array in Ruby?

You can use either new_array = array.reject {|x| x < 3} (reject returns a new array) or array.reject! {|x| x < 3} (reject! aka delete_if modifies the array in place).

There's also the (somewhat more common) select method, which acts like reject except that you specify the condition to keep elements, not to reject them (i.e. to get rid of the elements less than 3, you'd use new_array = array.select {|x| x >= 3}).

Removing array elements when string matches tags

@steenslag gave me the idea that offered up the solution for my problem.

resources.delete_if do |resource| 
(resource.tags.map(&:name) & no_tags).present?
end


Related Topics



Leave a reply



Submit