Get common entries in two arrays

Hi, is there a way to generate a new array with the common elements in
array A and arra B? This is:

arrayA = [“qqq”, “www”, “eee”]
arrayB = [“www”, “eee”, “rrr”, “ttt”]

the result should be: [“www”, “eee”]

Thanks a lot.

2011/7/19 Iñaki Baz C. [email protected]:

Hi, is there a way to generate a new array with the common elements in
array A and arra B? This is:

arrayA = [“qqq”, “www”, “eee”]
arrayB = [“www”, “eee”, “rrr”, “ttt”]

the result should be: [“www”, “eee”]

Of course I mean a method which perhaps I miss in Array class. If not,
I can do it manually :slight_smile:

arrayA | arrayB should do the trick

2011/7/19 Iaki Baz C. [email protected]

On Tue, Jul 19, 2011 at 12:14 PM, Iaki Baz C. [email protected]
wrote:

Hi, is there a way to generate a new array with the common elements in
array A and arra B? This is:

arrayA = [“qqq”, “www”, “eee”]
arrayB = [“www”, “eee”, “rrr”, “ttt”]

the result should be: [“www”, “eee”]

ruby-1.8.7-p334 :001 > arrayA = [“qqq”, “www”, “eee”]
=> [“qqq”, “www”, “eee”]
ruby-1.8.7-p334 :002 > arrayB = [“www”, “eee”, “rrr”, “ttt”]
=> [“www”, “eee”, “rrr”, “ttt”]
ruby-1.8.7-p334 :003 > arrayA & arrayB
=> [“www”, “eee”]

Jesus.

On 19 juil. 2011, at 12:14, Iaki Baz C. wrote:

Hi, is there a way to generate a new array with the common elements in
array A and arra B? This is:

arrayA = [“qqq”, “www”, “eee”]
arrayB = [“www”, “eee”, “rrr”, “ttt”]

the result should be: [“www”, “eee”]

arrayC = arrayA & arrayB

El día 19 de julio de 2011 12:19, Jesús Gabriel y Galán
[email protected] escribió:

ruby-1.8.7-p334 :001 > arrayA = [“qqq”, “www”, “eee”]
=> [“qqq”, “www”, “eee”]
ruby-1.8.7-p334 :002 > arrayB = [“www”, “eee”, “rrr”, “ttt”]
=> [“www”, “eee”, “rrr”, “ttt”]
ruby-1.8.7-p334 :003 > arrayA & arrayB
=> [“www”, “eee”]

Ops, great :slight_smile:

Thanks a lot.

damn. arrayA & arrayB is it.

2011/7/19 Gunther D. [email protected]

On Tue, Jul 19, 2011 at 12:19 PM, Gunther D. [email protected]
wrote:

arrayA | arrayB should do the trick

That would be the union. He’s looking for Array#&

Jesus.