# Comparing Arrays

**URL:** <https://www.ruby-forum.com/t/comparing-arrays/186722>\
**Category:** Ruby\
**Created:** [April 12, 2010, 7:49am UTC](https://www.ruby-forum.com/t/comparing-arrays/186722 "2010-04-12T07:49:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 12, 2010, 7:49am UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/1 "2010-04-12T07:49:17Z")

</div>

Hello everyone, another easy question from a beginner: How do you create  
a method to compare two arrays so that if they share at least 1 common  
element it returns true?

For example:  
a = %w(m w f)  
b = %w(m w)  
c = %w(t r)

share\_elements?(a,b) #=\> true  
share\_elements?(a,c) #=\> false

Thanks for your time,  
Derek

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 12, 2010, 8:17am UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/2 "2010-04-12T08:17:23Z")

</div>

Hello Derek,

2010/4/12 Derek C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hello everyone, another easy question from a beginner: How do you create  
> a method to compare two arrays so that if they share at least 1 common  
> element it returns true?

Well, you could use exactly the have\_common? method I provided to  
compare ranges because arrays also have a to\_a method (that does  
nothing else than returning the array itself)

def have\_common?(r1,r2)  
arr = r1.to\_a & r2.to\_a  
!arr.empty?  
end

a = %w(m w f)  
b = %w(m w)  
c = %w(t r)  
have\_common?(a,b)  
have\_common?(a,c)

Cheers,

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 12, 2010, 8:25am UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/3 "2010-04-12T08:25:07Z")

</div>

Thanks Jean, this is the first thing I tried, but I accidentally used  
the word “and” instead of &.

What’s the difference between & and “and” in this case?

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 12, 2010, 8:50am UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/4 "2010-04-12T08:50:30Z")

</div>

Hi,  
“&” is bitwise operator. “and” is logical operator & for composition.  
that’s main difference between “&” and “and”. so, as you need to compare  
array element use &.

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 12, 2010, 8:51am UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/5 "2010-04-12T08:51:55Z")

</div>

Hello Derek,

2010/4/12 Derek C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks Jean, this is the first thing I tried, but I accidentally used  
> the word “and” instead of &.
> 
> What’s the difference between & and “and” in this case?

Well, they have quite nothing in common here :o)  
On one hand, you are thinking of && which is (almost) the same as  
‘and’ (except concerning precedence).  
On the other end, ‘&’ is here the intersection operator defined in the  
array class:

Chandler ~\>ri ‘Array.&’  
---------------------------------------------------------------- Array#&  
array & other\_array

```
 From Ruby 1.8

```

* * *

```
 Set Intersection---Returns a new array containing elements common
 to the two arrays, with no duplicates.

    [1, 1, 3, 5] & [1, 2, 3] #=> [1, 3]

```

Cheers,

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 12, 2010, 8:56am UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/6 "2010-04-12T08:56:02Z")

</div>

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

> share\_elements?(a,c) #=\> false
> 
> ## Thanks for your time, Derek
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

An answer:

# array\_cmp.rb

def share\_elements?(a1,a2)  
not (a1 & a2).empty?  
end

a = %w(m w f)  
b = %w(m w)  
c = %w(t r)

r = share\_elements?(a,b) #=\> true  
printf(“%12s/%-12s =\> %s\n”, a.inspect.to\_s, b.inspect.to\_s, r.to\_s)  
r = share\_elements?(a,c) #=\> false  
printf(“%12s/%-12s =\> %s\n”, a.inspect.to\_s, c.inspect.to\_s, r.to\_s)  
exit(0)

Hth gfb

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [May 16, 2010, 4:13pm UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/7 "2010-05-16T16:13:31Z")

</div>

On 12.04.2010 08:50, Priyanka P. wrote:

> “&” is bitwise operator.  
> Except there’s nothing bitwise about & on arrays (or anything else that  
> isn’t an integer)

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [May 16, 2010, 11:47am UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/8 "2010-05-16T11:47:09Z")

</div>

good job done!!

* * *

frmsrcurl: [http://compgroups.net/comp.lang.ruby/Comparing-Arrays](http://compgroups.net/comp.lang.ruby/Comparing-Arrays)

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [May 16, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/comparing-arrays/186722/9 "2010-05-16T17:24:56Z")

</div>

Hi,

> 

Watch it, though, the comparison is based on Object#hash:

irb for ruby-1.9.3-r27796

> class N  
> attr\_reader :n  
> def initiaize(n)  
> @n = n  
> end  
> end  
> [N.new(1), N.new(2)] & [N.new(2), N.new(3)]  
> =\> []  
> [N.new(2), N.new(2)].map(&:hash)  
> =\> [2562895216144860017, -4480771430923711611]

So, to make it works as expected:

> class N  
> def hash  
> @n.hash  
> end  
> def eql?(n)  
> @n == n.n  
> end  
> end  
> [N.new(2), N.new(2)].map(&:hash)  
> =\> [-1851237208519162443, -1851237208519162443]  
> [N.new(1), N.new(2)] & [N.new(2), N.new(3)]  
> =\> [#\<N:0x00000101161b40\>] # 2

It sounds like a bug to me this …  
(or at least documentation not clear:  
“Set Intersection—Returns a new array containing elements common to  
the two  
arrays, with no duplicates.”)

Programming Ruby is more complete:  
“Set Intersection—Returns a new array containing elements common to  
the two arrays, with no duplicates.(identical)  
The rules for comparing elements are the same as for hash keys. If you  
need setlike behavior, see the library class Set”

then why is it called Set Intersection?  
(anyway, Set#& behaves the same)
