# Find if an array has any element present in another array

**URL:** <https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877>\
**Category:** Ruby\
**Created:** [April 30, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877 "2009-04-30T12:52:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 30, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877/1 "2009-04-30T12:52:24Z")

</div>

Hi,

I have 2 lists

a=[1,2,3,4]  
b=[2,4,6,7,8]

I want to find if there are any elements present in “a” which are also  
present in “b”.  
I jus wanna “true” or “false” answer if both of the arrays have any  
element in common.

Is there any inbuilt functions like “include?” for this purpose??

Thanks in Advance.

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 30, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877/2 "2009-04-30T12:59:52Z")

</div>

Charanya N. wrote:

> Hi,
> 
> I have 2 lists
> 
> a=[1,2,3,4]  
> b=[2,4,6,7,8]
> 
> I want to find if there are any elements present in “a” which are also  
> present in “b”.  
> I jus wanna “true” or “false” answer if both of the arrays have any  
> element in common.
> 
> Is there any inbuilt functions like “include?” for this purpose??
> 
> Thanks in Advance.

a=[1,2,3,4]  
b=[2,4,6,7,8]

result = a & b  
p result

puts (a & b).empty?

–output:–  
[2, 4]  
false

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 30, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877/3 "2009-04-30T13:02:07Z")

</div>

Le Thu, 30 Apr 2009 05:52:26 -0500,  
Charanya N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) a Ã©crit :

> I have 2 lists
> 
> a=[1,2,3,4]  
> b=[2,4,6,7,8]
> 
> I want to find if there are any elements present in “a” which are also  
> present in “b”.  
> I jus wanna “true” or “false” answer if both of the arrays have any  
> element in common.

(a-b).nil?

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 30, 2009, 1:05pm UTC](https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877/4 "2009-04-30T13:05:41Z")

</div>

2009/4/30 Charanya N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I have 2 lists
> 
> a=[1,2,3,4]  
> b=[2,4,6,7,8]
> 
> I want to find if there are any elements present in “a” which are also  
> present in “b”.  
> I jus wanna “true” or “false” answer if both of the arrays have any  
> element in common.

You can take the intersection of the two and see if it’s empty.

if (a & b).empty?  
puts( ‘Nothing in common’ )  
else  
puts( ‘Common elements’)  
end

Farrel

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 30, 2009, 1:11pm UTC](https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877/5 "2009-04-30T13:11:42Z")

</div>

Thank you.This was What i wanted

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 30, 2009, 1:13pm UTC](https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877/6 "2009-04-30T13:13:42Z")

</div>

as wrote:

> (a-b).nil?

result = [1] - [2, 3] #no common elements  
p result

result = [2, 3] - [2] #common elements  
p result

–output:–  
[1]  
[3]

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 30, 2009, 1:19pm UTC](https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877/7 "2009-04-30T13:19:13Z")

</div>

Le Thu, 30 Apr 2009 12:57:23 +0200,  
as [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) a Ã©crit :

> > I jus wanna “true” or “false” answer if both of the arrays have any  
> > element in common.
> 
> (a-b).nil?

Ouch, sorry, (a&b).empty?
