Reputation: 399
I've got an Array
of arrays. There can be two or more subarrays:
array = [
["66892885", "66891801", "66924833", "66892255"],
["65167829", "65167828", "66924833"],
["66924833", "66891801"]
]
I need only the values found across each subarray. So in this case "66924833"
would be the only match. For a value to show up in the result, each subarray has to contain it.
How can I do this?
Upvotes: 2
Views: 233
Reputation: 2783
You can combine inject with Array's Set Intersection (#&
) method like this
array.inject(:&)
to get the desired result:
array=[["66892885", "66891801", "66924833", "66892255", "1", "33"],
["65167829", "65167828", "66924833", "1", "33", "44"],
["2344", "66924833", "1", "33"]]
array.inject(:&)
#=>["66924833", "1", "33"]
Upvotes: 3