Ruby Language Arrays Arrays union, intersection and difference

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

x = [5, 5, 1, 3]
y = [5, 2, 4, 3]

Union (|) contains elements from both arrays, with duplicates removed:

x | y
=> [5, 1, 3, 2, 4]

Intersection (&) contains elements which are present both in first and second array:

x & y
=> [5, 3]

Difference (-) contains elements which are present in first array and not present in second array:

x - y
=> [1]


Got any Ruby Language Question?