# Find a set of common elements from a multi-dimensional array

**URL:** <https://www.ruby-forum.com/t/find-a-set-of-common-elements-from-a-multi-dimensional-array/186401>\
**Category:** Ruby\
**Created:** [April 6, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/find-a-set-of-common-elements-from-a-multi-dimensional-array/186401 "2010-04-06T14:50:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shreyas12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@shreyas12](https://www.ruby-forum.com/u/shreyas12)\
**Post date:** [April 6, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/find-a-set-of-common-elements-from-a-multi-dimensional-array/186401/1 "2010-04-06T14:50:19Z")

</div>

I have a multi-dimensional array ; a=[[2,3,4],[1,3,4],[1,2],[1,2,3,4]]  
i’ve to compare all the 4 sub-arrays and get common elements.Next,take 3  
subarrays at a time and get common elements.then take 2 sub arrays at a  
time and get common elements.

---

<div class="post-metadata">

**Author:** ![shreyas12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@shreyas12](https://www.ruby-forum.com/u/shreyas12)\
**Post date:** [April 6, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/find-a-set-of-common-elements-from-a-multi-dimensional-array/186401/2 "2010-04-06T15:02:36Z")

</div>

Hello,

2010/4/6 Shreyas S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I have a multi-dimensional array ; a=[[2,3,4],[1,3,4],[1,2],[1,2,3,4]]  
> i’ve to compare all the 4 sub-arrays and get common elements.Next,take 3  
> subarrays at a time and get common elements.then take 2 sub arrays at a  
> time and get common elements.

Have a look to Array#combination to group your subarrays into  
comparison sets and then use the Set class (require ‘set’) and it’s  
intersection instance method to get the common elements.

Cheers,

---

<div class="post-metadata">

**Author:** ![shreyas12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@shreyas12](https://www.ruby-forum.com/u/shreyas12)\
**Post date:** [April 6, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/find-a-set-of-common-elements-from-a-multi-dimensional-array/186401/3 "2010-04-06T15:05:32Z")

</div>

Shreyas S. wrote:

> I have a multi-dimensional array ; a=[[2,3,4],[1,3,4],[1,2],[1,2,3,4]]  
> i’ve to compare all the 4 sub-arrays and get common elements.Next,take 3  
> subarrays at a time and get common elements.then take 2 sub arrays at a  
> time and get common elements.

Well, the way to get common elements between arrays is with the  
“intersection” method:  
[class Array - RDoc Documentation](http://ruby-doc.org/core/classes/Array.html#M002212) (of course,  
available as a Set operation too:  
[http://ruby-doc.org/core/classes/Set.html#M001621](http://ruby-doc.org/core/classes/Set.html#M001621))

And what you’re looking for is a brute-force way to compare all subsets  
of all possible sizes with other subsets of the same size. I’ll leave  
that one as an exercise.
