Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
[1, 2, 3] & [2, 3, 4]给了我们[2, 3]但是你如何得到n个数组的交集?
[1, 2, 3] & [2, 3, 4]
[2, 3]
[[1, 2, 3], [2, 3, 4], [1, 3, 4]].something会给[3]
[[1, 2, 3], [2, 3, 4], [1, 3, 4]].something
[3]
循环&工作,但必须有更好的方法。
&
[[1, 2, 3], [2, 3, 4], [1, 3, 4]].inject(:&) #=> [3]
只是&所有数组。假设您有 3 个数组。
a = [1,2,3] b = [2,3,4] c = [3,4,5] a & b & c => [3]