The Easiest way to check if one array is a subset of another

I want to find the easiest way if one array is a subset of another

for example

A b
[1,2,4,5,6,7,9] [1,3,9] =>true

A b
[1,2,4,5,6,7,9] [1,3,8] =>false

Oop, i already found

http://adzdavies.blogspot.com/2008/02/array-subsets-and-supersets.html

b.to_set.subset?(a.to_set)

1 Like

A b
[1,2,4,5,6,7,9] [1,3,9] =>true

A b
[1,2,4,5,6,7,9] [1,3,8] =>false

Posted via http://www.ruby-forum.com/.

If I understand correctly what you really want, the first example should
produce false, as array “a” does not have 3. Assuming that you meant
[1,2,3,4,5,6,7,9] I think the following will work for you:

(a & b) == b

Gennady.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs