Reputation: 371
I have an array of arrays [[1,2,3],[4,5,6],[7,8,9]]
. I also have an array of integers [3,4,5,6,8]
.
Is it possible for me to check if my integers match a complete array in the array of arrays?
So I have 4,5,6
in the int array, and it matches the middle array [4,5,6]
.
Upvotes: 2
Views: 91
Reputation: 4032
Try this:
array_1 = [[1,2,3],[4,5,6],[7,8,9]]
array_2 = [3,4,5,6,8]
array_1.any? { |e| (e - array_2).empty? }
# => true
Upvotes: 1
Reputation: 1630
Assuming you expect a true
or false
and order doesn't matter, the following works:
require 'set'
a1 = [[1,2,3],[4,5,6],[7,8,9]]
a2 = [3,4,5,6,8]
a1.any? { |item| item.to_set.subset? a2.to_set } #=> true
Assuming you want the index
into a1
or nil
a1.index { |item| item.to_set.subset? a2.to_set }
Assuming you want the subset itself or nil
index = a1.index { |item| item.to_set.subset? a2.to_set }
index && a1[index]
Upvotes: 0
Reputation: 30056
This should work
a = [[1,2,3],[4,5,6],[7,8,9]]
integers = [3,4,5,6,8]
a.any? { |sub_array| sub_array.all? { |item| integers.include? item } }
Upvotes: 2
Reputation: 3310
array1 = [[1,2,3],[4,5,6],[7,8,9]]
array2 = [4,5,6]
result = array1.map{|inner_array| inner_array - array2}
# => [[1, 2, 3], [], [7, 8, 9]]
result.any?{|inner_array| inner_array.empty?}
# => true
Upvotes: 0