# Match at least one element of array to another

**URL:** <https://www.ruby-forum.com/t/match-at-least-one-element-of-array-to-another/186587>\
**Category:** Ruby\
**Created:** [April 9, 2010, 9:35am UTC](https://www.ruby-forum.com/t/match-at-least-one-element-of-array-to-another/186587 "2010-04-09T09:35:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kentorwow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kentorwow](https://www.ruby-forum.com/u/kentorwow)\
**Post date:** [April 9, 2010, 9:35am UTC](https://www.ruby-forum.com/t/match-at-least-one-element-of-array-to-another/186587/1 "2010-04-09T09:35:49Z")

</div>

Hi is there a simple way to return true if at least one element of an  
array matches at least one element of another array?

For example let’s say I have @groups = ‘4,6’.split(’,’) and @allowed =  
‘1,4’.split(’,’)

I want these to be true because 4 is in both @groups and @allowed.

Thank you!

---

<div class="post-metadata">

**Author:** ![kentorwow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kentorwow](https://www.ruby-forum.com/u/kentorwow)\
**Post date:** [April 9, 2010, 9:48am UTC](https://www.ruby-forum.com/t/match-at-least-one-element-of-array-to-another/186587/2 "2010-04-09T09:48:47Z")

</div>

Hmm I came up with a very inelegant way to do this… that is:

@groups.length + @allowed.length \> (@groups | @allowed).length

which is equivalent to 4 \> 3 so it evaluates to true.

I guess this works but if you come up with a more elegant way please  
share! Thanks.

---

<div class="post-metadata">

**Author:** ![kentorwow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kentorwow](https://www.ruby-forum.com/u/kentorwow)\
**Post date:** [April 9, 2010, 9:55am UTC](https://www.ruby-forum.com/t/match-at-least-one-element-of-array-to-another/186587/3 "2010-04-09T09:55:03Z")

</div>

Okay never mind just came up with a better way:  
!(@groups & @allowed).empty?  
=\> true

---

<div class="post-metadata">

**Author:** ![kentorwow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kentorwow](https://www.ruby-forum.com/u/kentorwow)\
**Post date:** [April 9, 2010, 10:13am UTC](https://www.ruby-forum.com/t/match-at-least-one-element-of-array-to-another/186587/4 "2010-04-09T10:13:50Z")

</div>

Hello,

> I want these to be true because 4 is in both @groups and @allowed.

You could check something like

sum = @groups+@allowed  
if sum.uniq != sum  
what\_you\_want\_to\_do\_if\_same\_elements  
end

Example:

> > a,b,c = [1,2],[2,3],[4,5]  
> > =\> [[1, 2], [2, 3], [4, 5]]
> 
> > (a+b).uniq != (a+b)  
> > =\> true
> 
> > (a+c).uniq != (a+c)  
> > =\> false

Cheers

---

<div class="post-metadata">

**Author:** ![kentorwow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kentorwow](https://www.ruby-forum.com/u/kentorwow)\
**Post date:** [April 9, 2010, 10:41am UTC](https://www.ruby-forum.com/t/match-at-least-one-element-of-array-to-another/186587/5 "2010-04-09T10:41:20Z")

</div>

“Kenneth Eunjung” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

array1 & array2 returns a new\_ array with elements common to  
both arrays. Test on new\_array.size returns true if common  
elements exsist, false if not.

@groups = ‘4,6’.split(‘,’)  
@allowed = ‘1,4’.split(‘,’)  
a = @groups & @allowed # =\> [4]  
puts a.inspect.to\_s # =\> [4]  
ret = (a.size \> 0)  
puts ret.to\_s # =\> true  
b = [4,6] & [5,7]  
puts b.inspect.to\_s # =\> []  
ret = (b.size \> 0)  
puts ret.to\_s # =\> false  
exit

Hth gfb
