数组包含来自另一个数组的任何值? - Array include any value from another array?

问题:

What's the most efficient way to test if an array contains any element from a second array?测试数组是否包含来自第二个数组的任何元素的最有效方法是什么?

Two examples below, attempting to answer the question does foods contain any element from cheeses :下面的两个例子试图回答这个问题, foods含有cheeses任何元素:

cheeses = %w(chedder stilton brie mozzarella feta haloumi reblochon)
foods = %w(pizza feta foods bread biscuits yoghurt bacon)

puts cheeses.collect{|c| foods.include?(c)}.include?(true)

puts (cheeses - foods).size < cheeses.size

解决方案:

参考一: https://en.stackoom.com/question/GXTl
参考二: https://stackoom.com/question/GXTl
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章