# Is there a quick way to compare two arrays and

**URL:** <https://www.ruby-forum.com/t/is-there-a-quick-way-to-compare-two-arrays-and/60616>\
**Category:** Ruby\
**Created:** [January 9, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/is-there-a-quick-way-to-compare-two-arrays-and/60616 "2006-01-09T21:54:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 9, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/is-there-a-quick-way-to-compare-two-arrays-and/60616/1 "2006-01-09T21:54:24Z")

</div>

I have two arrays of different sizes.  
Right now via find\_all I can find all the elements that are common to  
both  
arrays.  
All the elements in the smaller arrays can be found in the larger array.

What I would like to find now is all th elements that are in the larger  
array and missing on the smaller array.  
I know I can iterate and compare each element within a nested loop.  
But is there a way to print out the elements that are missing from the  
smaller array without having to implement a nested loop?

Thank you

Victor

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 9, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/is-there-a-quick-way-to-compare-two-arrays-and/60616/2 "2006-01-09T22:03:29Z")

</div>

On Jan 9, 2006, at 12:53 PM, Victor R. wrote:

> I know I can iterate and compare each element within a nested loop.  
> But is there a way to print out the elements that are missing from the  
> smaller array without having to implement a nested loop?
> 
> Thank you
> 
> Victor

Like this?

irb(main):009:0\> a = [1,2,3,4,5]  
=\> [1, 2, 3, 4, 5]  
irb(main):010:0\> b = [1,2,3]  
=\> [1, 2, 3]  
irb(main):011:0\> a - b  
=\> [4, 5]

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 10, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/is-there-a-quick-way-to-compare-two-arrays-and/60616/3 "2006-01-10T14:38:36Z")

</div>

Ezra,  
Thanks a bunch.  
This is soooooooooo simple that it is laughable.  
I guess I need to read my Ruby books a bit more.

Thanks again

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 11, 2006, 6:47am UTC](https://www.ruby-forum.com/t/is-there-a-quick-way-to-compare-two-arrays-and/60616/4 "2006-01-11T06:47:10Z")

</div>

Shame not, young grasshopper, “substracting” arrays only really makes  
perfect sense after you know it’s there. Isn’t there a verbose name for  
the method? I know Java had a removeAll(Collection) method on one of the  
collection interfaces since gods know when, so it’s not really that  
Obscure Voodoo ™.

David V.
