Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ruby中找到n个数组的交集?_Ruby_Arrays - Fatal编程技术网

如何在Ruby中找到n个数组的交集?

如何在Ruby中找到n个数组的交集?,ruby,arrays,Ruby,Arrays,[1,2,3]&[2,3,4]给出了[2,3]但是如何获得n个数组的交集呢 [[1,2,3],[2,3,4],[1,3,4]。会给[3] 使用&循环是可行的,但必须有更好的方法。仅使用&all数组。假设有3个数组 [[1, 2, 3], [2, 3, 4], [1, 3, 4]].inject(:&) #=> [3] a = [1,2,3] b = [2,3,4] c = [3,4,5] a & b & c => [3]

[1,2,3]&[2,3,4]
给出了
[2,3]
但是如何获得n个数组的交集呢

[[1,2,3],[2,3,4],[1,3,4]。
会给
[3]


使用
&
循环是可行的,但必须有更好的方法。

仅使用&all数组。假设有3个数组

[[1, 2, 3], [2, 3, 4], [1, 3, 4]].inject(:&) #=> [3]
a = [1,2,3]
b = [2,3,4]
c = [3,4,5]

a & b & c
=> [3]