問題:
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