Toby Joiner
Toby Joiner

Reputation: 4376

get matching items in arrays

I found another question on here that told how to get the matching items in 2 arrays like this:

matches = array1 & array2

However I have an array of arrays. like:

[[1,2,3,4],[2,3,4,5],[1,3,4,5]]

In this case I want to return 3 and 4 because they are in all three arrays.

How do I go about doing that?

Thank you!

Upvotes: 0

Views: 226

Answers (1)

mu is too short
mu is too short

Reputation: 434945

Like this:

a.reduce(:&)

For example:

>> a = [[1,2,3,4],[2,3,4,5],[1,3,4,5]]
=> [[1, 2, 3, 4], [2, 3, 4, 5], [1, 3, 4, 5]]
>> a.reduce(:&)
=> [3, 4]

Upvotes: 7

Related Questions