开发者

get matching items in arrays

开发者 https://www.devze.com 2023-04-11 07:25 出处:网络
I found another question on here that told how to get the matching items in 2 arrays l开发者_StackOverflowike this:

I found another question on here that told how to get the matching items in 2 arrays l开发者_StackOverflowike 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!


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]
0

精彩评论

暂无评论...
验证码 换一张
取 消