# Compare Arrays

**URL:** <https://www.ruby-forum.com/t/compare-arrays/150729>\
**Category:** Ruby\
**Created:** [October 12, 2008, 5:53am UTC](https://www.ruby-forum.com/t/compare-arrays/150729 "2008-10-12T05:53:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 12, 2008, 5:53am UTC](https://www.ruby-forum.com/t/compare-arrays/150729/1 "2008-10-12T05:53:12Z")

</div>

Hey, I just have a quick question about arrays.

I have two different ones that I would like to compare.

Example:

Array 1: cats, dogs, monkeys

Array 2: cats, fish, dogs, birds, monkeys

Now, I would like to compare array 2 with array 1 and have it spit out  
any words that aren’t in both… if that makes sense.

So what the program should output is fish, monkeys.

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 12, 2008, 7:00am UTC](https://www.ruby-forum.com/t/compare-arrays/150729/2 "2008-10-12T07:00:43Z")

</div>

On Sat, Oct 11, 2008 at 10:50 PM, Tj Superfly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Now, I would like to compare array 2 with array 1 and have it spit out  
> any words that aren’t in both… if that makes sense.
> 
> So what the program should output is fish, monkeys.

Hmm. I’m assuming you are looking for [“fish”, “birds”], and if you  
are, do union, then subtract intersection…

a = %w( cats dogs monkeys )  
b = %w( cats fish dogs birds monkeys )

(a | b) - (a & b)

More than two arrays is left as an exercise 🙂

Todd

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 12, 2008, 7:03am UTC](https://www.ruby-forum.com/t/compare-arrays/150729/3 "2008-10-12T07:03:38Z")

</div>

You are doing set operations, so I looked there first:  
require ‘set’  
s1 = Set.new [1, 2, 7, 8, 9, 45, 98, 85, 39]  
s2 = Set.new [1, 45, 66, 4, 84, 98]

p (s1 - s2) | (s2 - s1)

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 12, 2008, 7:05am UTC](https://www.ruby-forum.com/t/compare-arrays/150729/4 "2008-10-12T07:05:23Z")

</div>

On Oct 11, 11:50 pm, Tj Superfly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now, I would like to compare array 2 with array 1 and have it spit out  
> any words that aren’t in both… if that makes sense.
> 
> ## So what the program should output is fish, monkeys.
> 
> Posted viahttp://www.ruby-forum.com/.

Im assuming the elements in the array are strings so…

so we have arr1 and arr2 with the previous mentioned elements i guess  
you could make a method something like this and then depending on how  
your comparing these elements…

def print\_elements\_not\_in\_array  
arr1.each do |temp|  
arr2.each do |temp2|  
puts " #{temp2} " if temp1 != temp2  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 12, 2008, 11:23am UTC](https://www.ruby-forum.com/t/compare-arrays/150729/5 "2008-10-12T11:23:46Z")

</div>

Tj Superfly wrote:

> Hey, I just have a quick question about arrays.
> 
> I have two different ones that I would like to compare.
> 
> Example:
> 
> Array 1: cats, dogs, monkeys
> 
> Array 2: cats, fish, dogs, birds, monkeys
> 
> Now, I would like to compare array 2 with array 1 and have it spit out  
> any words that aren’t in both… if that makes sense.
> 
> So what the program should output is fish, monkeys.

If arr2 is always a superset of arr1, then arr2-arr1

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 12, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/compare-arrays/150729/6 "2008-10-12T16:41:26Z")

</div>

Todd B. schrieb:

> > 
> 
> (a | b) - (a & b)

Why not just

b - a

? But as I understand him what he wants is

a & b

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 12, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/compare-arrays/150729/7 "2008-10-12T18:07:00Z")

</div>

Hi –

On Sun, 12 Oct 2008, Rüdiger Brahns wrote:

> > > 
> > 
> > a = %w( cats dogs monkeys )  
> > b = %w( cats fish dogs birds monkeys )
> > 
> > (a | b) - (a & b)
> 
> Why not just
> 
> b - a

As Brian C. said, that works but only if b is always a superset of a.  
If not, you get:

[1,2,3] - [2,3,4] # [1], should be [1,4]

> ? But as I understand him what he wants is
> 
> a & b

I think he wants:

a ^ b # exclusive or

which unfortunately doesn’t exist 🙂 It’s basically

(a | b) - (a & b)

which I think someone else posted. Or, if you don’t want to create  
that many intermediate arrays, you could do:

module EnumExOr  
def ^(other)  
reject {|e| other.include?(e) }.  
concat(other.reject {|e| include?(e) })  
end  
end

array = [1,2,3,4,5].extend(EnumExOr)

p array ^ [3,4,5,6,7] # [1,2,6,7]

David
