2

[1, 2, 3] & [2, 3, 4] gives us [2, 3] but how do you get the intersection of n arrays?

[[1, 2, 3], [2, 3, 4], [1, 3, 4]].something would give [3]

Looping with & works but there must be a better way.

2 Answers 2

8
[[1, 2, 3], [2, 3, 4], [1, 3, 4]].inject(:&) #=> [3] 
Sign up to request clarification or add additional context in comments.

Comments

0

Just & all arrays. Suppose you have 3 arrays.

a = [1,2,3] b = [2,3,4] c = [3,4,5] a & b & c => [3] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.