數組包含來自另一個數組的任何值? - 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章