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
:
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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…