Get the intersection of 2 arrays

Pretty easy stuff today, but this is not something you may need to do often so maybe you don’t know how to do it.
Say I have these 2 arrays :

colors1 = [:blue, :red, :green, :orange, :purple]
colors2 = [:yellow, :cyan, :green, :blue, :purple]

Now what if I want a new array that contains only the elements present in both arrays?

beautiful_colors = colors1 & colors2
#beautiful_colors contains [:blue, :green, :purple]

Have a nice weekend!

Leave a Reply

Your email address will not be published. Required fields are marked *