# Best way to select uncommon elements from array

**URL:** <https://www.ruby-forum.com/t/best-way-to-select-uncommon-elements-from-array/183541>\
**Category:** Rails\
**Created:** [February 16, 2010, 9:59am UTC](https://www.ruby-forum.com/t/best-way-to-select-uncommon-elements-from-array/183541 "2010-02-16T09:59:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahtha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@rahtha](https://www.ruby-forum.com/u/rahtha)\
**Post date:** [February 16, 2010, 9:59am UTC](https://www.ruby-forum.com/t/best-way-to-select-uncommon-elements-from-array/183541/1 "2010-02-16T09:59:57Z")

</div>

I have two arrays and i want to get all items which are NOT common to  
both, so basically if i have two arrays A and B, i want all elements  
in A that are not in B. what is the best way to do this apart from  
using two nested loops.

thanks  
rahtha

---

<div class="post-metadata">

**Author:** ![rahtha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@rahtha](https://www.ruby-forum.com/u/rahtha)\
**Post date:** [February 16, 2010, 10:04am UTC](https://www.ruby-forum.com/t/best-way-to-select-uncommon-elements-from-array/183541/2 "2010-02-16T10:04:03Z")

</div>

On Feb 16, 7:10 am, rahtha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have two arrays and i want to get all items which are NOT common to  
> both, so basically if i have two arrays A and B, i want all elements  
> in A that are not in B. what is the best way to do this apart from  
> using two nested loops.

A - B ?

---

<div class="post-metadata">

**Author:** ![rahtha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@rahtha](https://www.ruby-forum.com/u/rahtha)\
**Post date:** [February 16, 2010, 5:04pm UTC](https://www.ruby-forum.com/t/best-way-to-select-uncommon-elements-from-array/183541/3 "2010-02-16T17:04:20Z")

</div>

On Tue, Feb 16, 2010 at 8:10 AM, rahtha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have two arrays and i want to get all items which are NOT common to  
> both, so basically if i have two arrays A and B, i want all elements  
> in A that are not in B. what is the best way to do this apart from  
> using two nested loops.

But do you want the ones in B that are not in A as well? (symmetric  
difference)

---

<div class="post-metadata">

**Author:** ![rahtha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@rahtha](https://www.ruby-forum.com/u/rahtha)\
**Post date:** [February 16, 2010, 6:38pm UTC](https://www.ruby-forum.com/t/best-way-to-select-uncommon-elements-from-array/183541/4 "2010-02-16T18:38:00Z")

</div>

Nope don’t care about that case just the ones in A that are not in B

---

<div class="post-metadata">

**Author:** ![rahtha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@rahtha](https://www.ruby-forum.com/u/rahtha)\
**Post date:** [February 16, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/best-way-to-select-uncommon-elements-from-array/183541/5 "2010-02-16T19:09:53Z")

</div>

rahtha wrote:

> Nope don’t care about that case just the ones in A that are not in B

Well, then it’s what Frederick said.

irb(main):065:0\> a = [1,2,3]  
=\> [1, 2, 3]  
irb(main):066:0\> b = [2,4,6]  
=\> [2, 4, 6]  
irb(main):067:0\> a-b  
=\> [1, 3]
