Extract common entries in two arrays


#1

Hi, how to get an array with just common entries from two arrays?:

array1 = [“q”, “w”, “e”, “r”, “t”, “y”]
array2 = [“w”, “t”, “z”]

I want to get:

array_common = [“w”, “t”]

I don0t find an ellegant and fast way to get it. Any help?
Thanks a lot.


#2

El Domingo, 15 de Marzo de 2009, Iñaki Baz C. escribió:

Hi, how to get an array with just common entries from two arrays?:

array1 = [“q”, “w”, “e”, “r”, “t”, “y”]
array2 = [“w”, “t”, “z”]

I want to get:

array_common = [“w”, “t”]

array_common = array1 - (array1 - array2)
=> [“w”, “t”]

:slight_smile:


#3

On Sat, Mar 14, 2009 at 4:34 PM, Iñaki Baz C. removed_email_address@domain.invalid wrote:

array1 = [“q”, “w”, “e”, “r”, “t”, “y”]
array2 = [“w”, “t”, “z”]

I want to get:
array_common = [“w”, “t”]

array_common = array1 - (array1 - array2)
=> [“w”, “t”]

Easier:

array1 & array2
=> [“w”, “t”]

Ben


#4

El Domingo, 15 de Marzo de 2009, Ben B.
escribió:> Easier:

array1 & array2
=> [“w”, “t”]

Great!


#5

On Sun, Mar 15, 2009 at 8:29 AM, Iñaki Baz C. removed_email_address@domain.invalid wrote:

Thanks a lot.


Iñaki Baz C.

array1 & array2

Harry