s c s c - 1 year ago 110
Ruby Question

Ruby 2D array comparison

We have a 2D array as follows where A, B etc are the literals [string values]:

arr1 = [["A","B"], ["C","D"], ["E","F"], ["G","H"]]

now i want to compare this
with another array and remove the matching elements in the array, the other array is follows:

arr2 = [["C"], ["F"]]

i want to make sure that if any of the element matches with the element in
, the corresponding element should be removed from the
and output should be as follows:

output = [["A","B"], ["G","H"]]

Answer Source
a2 = arr2.flatten
  #=> ["C", "F"] 

arr1.reject { |a| (a & a2).any? } 
  #=> [["A", "B"], ["G", "H"]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download