16

Is there any method to check if array A contains all the elements of array B?

3
  • 2
    Show an example of arrays you want to compare. Do you mean that Array A can contain the same elements with the same number of elements, or just the same elements and different count of them Commented May 4, 2011 at 22:55
  • 2
    Do you care about repetition? For example, let a be [1,2,3,4] and b be [1,1,2]. What should the return value be? Commented May 5, 2011 at 7:33
  • Ruby's introduced difference in 2.6 which provides a perfect fast and readable solution for this. More info here. Commented Jun 24, 2019 at 15:24

8 Answers 8

19

You can try this

a.sort.uniq == b.sort.uniq

or

(a-b).empty?

And if [1,2,2] != [1,2] in your case you can:

a.group_by{|i| i} == b.group_by{|i| i}
Sign up to request clarification or add additional context in comments.

6 Comments

+1 for (a-b).empty?
Just in case someone ends up here: these are all wrong. The correct answer is (b-a).empty?, not the other way around. Others are just plain wrong. See also stackoverflow.com/questions/7387937/…
@oseiskar no, they are not wrong. It depends on the task.
I don't think so. If the task is to answer the question does array A contain all elements of array B, which quite literally translates to Ruby as B.map{|e|A.include?(e)}.all?, then none of these give the correct answer in the generic case (or, e.g., the first example I came up with: A = [1,2,3], B = [1,2]).
@oseiskar yes, this sounds reasonable. In generic case they all wrong.
|
9

This should work for what you need:

(a & b) == b

4 Comments

this won't work as a rule. a = [1,2,2,1,3]; b = [3,2,1]; (a & b) == b => false
Yes, but a = [1,2,2,1,3]; b = [3,2,1]; (a & b).sort == b.sort => true
@the Tin Man, Yes, but a = [1,2,2,1,3]; b = [3,2,2,1]; (a & b).sort == b.sort => false
no need to sort just reverse the order of the binary AND
7

You could use Ruby's Set class:

>> require 'set' #=> true
>> a = [*1..5] #=> [1, 2, 3, 4, 5]
>> b = [*1..3] #=> [1, 2, 3]
>> a.to_set.superset? b.to_set #=> true

For small arrays I usually do the same as what fl00r suggested:

>> (b-a).empty? #=> true

Comments

6

I prefer to do this via: (b - a).blank? # tells that b is contained in a

Comments

5

The simplest way is this:

(b-a).empty?

Comments

3

There's also the Set class (part of the standard library) which would allow you to just check to see if B is a subset of A, e.g.

>> a = [1,2,3,4,5]       => [1, 2, 3, 4, 5]
>> b = [3,4,5]           => [3, 4, 5]
>> require 'set'         => true 
>> set_a = a.to_set      => #<Set: {1, 2, 3, 4, 5}> 
>> set_b = b.to_set      => #<Set: {3, 4, 5}> 

>> set_b.subset? set_a   => true

http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/index.html

Comments

3

Ruby 2.6+

Ruby's introduced difference in 2.6 for exactly this purpose.

Very fast, very readable, as follows:

a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3, 4, 5, 6]

a.difference(b).any?
# => false
a.difference(b.reverse).any?
# => false

a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3]
a.difference(b).any?
# => true

Hope that helps someone!

Comments

1

You may want to check out the Set class in the Ruby Standard library. The proper_subset? method will probably do what you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.