How can I get the intersection, union, and subset of arrays in Ruby?

I want to create different methods for a class called Multiset.

I have all the required methods, but I’m unsure of how to write intersection, union, and subset methods.

For intersection and union, my code starts like this:

def intersect(var)
  x = Multiset.new
end

Here is an example:

X = [1, 1, 2, 4]
Y = [1, 2, 2, 2]

then the intersection of X and Y is [1, 2].

3 Answers
3

Leave a Comment