Do you ever find yourself programming in ruby, and you have two arrays, and you want to compare them against each other? Here's a couple of helpful array operators you can use that will show you which elements are common to two arrays, which are different, and more:
- array1 = ["x", "y", "z"]
- array2 = ["w", "x", "y"]
- array1 | array2 # Combine Arrays & Remove Duplicates(Union)
- => ["x", "y", "z", "w"]
- array1 & array2 # Get Common Elements between Two Arrays(Intersection)
- => ["x", "y"]
- array1 - array2 # Remove Any Elements from Array 1 that are contained in Array 2.(Difference)
- => ["z"]
Comments
0 comments
Please sign in to leave a comment.