0

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!

1 Answer 1

7

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] 
Sign up to request clarification or add additional context in comments.

2 Comments

This is why i love ruby and rails.
@TobyJoiner Nope, that's why you love ruby!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.