Ruby - Merge two arrays and remove values that have duplicate Ruby - Merge two arrays and remove values that have duplicate arrays arrays

Ruby - Merge two arrays and remove values that have duplicate


Use Array#uniq.

a = [1, 3, 5, 6]b = [2, 3, 4, 5]c = (a + b).uniq=> [1, 3, 5, 6, 2, 4]


You can do the following!

# Mergingc = a + b => [1, 2, 3, 4, 5, 2, 4, 6]# Removing the value of other array# (a & b) is getting the common element from these two arraysc - (a & b)=> [1, 3, 5, 6]

Dmitri's comment is also same though I came up with my idea independently.


How about this.

(a | b)=> [1, 2, 3, 4, 5, 6](a & b)=> [2, 4](a | b) - (a & b)[1, 3, 5, 6]

Documentation for | method
Documentation for & method