Ruby Array : Comparison

Lets start with two arrays
array1 = ["x", "y", "z"]  
array2 = ["w", "x", "y"]
Tip 1: Add two arrays and remove duplicates
array1 | array2
=> ["x", "y", "z", "w"]
Tip 2: Keeping common elements and remove duplicates
array1 & array2
=> ["x", "y"]
Tip 3 : Keeping unique elements
array1 - array2
=> ["z"]

Comments

Popular posts from this blog

Inserting and Moving elements inside Ruby Array

Interview Question on Ruby on Rails