Comparing Arrays

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

Hello Derek,

2010/4/12 Derek C. [email protected]:

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,

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?

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 &.

Hello Derek,

2010/4/12 Derek C. [email protected]:

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,

“Derek C.” [email protected] wrote in message
news:[email protected]

share_elements?(a,c) #=> false

Thanks for your time,
Derek

Posted via 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

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)

good job done!!


frmsrcurl: http://compgroups.net/comp.lang.ruby/Comparing-Arrays

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)