问题:
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