Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I filter elements in an array?

Tags:

arrays

ruby

Sample Array:

x = [1,2,3,4,2,2,2]

Filter:

y = [2,4,7,9]

Desired output:

result = [2,4,2,2,2]

I tried:

result = (x & y)

but this gives me [4,2].

How do I get: result = [2,4,2,2,2]?

like image 404
gkolan Avatar asked Jan 23 '26 01:01

gkolan


1 Answers

How about:

x - (x - y)
#=> [2, 4, 2, 2, 2]
like image 86
pguardiario Avatar answered Jan 25 '26 17:01

pguardiario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!