Array include any value from another array?

后端 未结 5 1973
青春惊慌失措
青春惊慌失措 2020-12-12 12:23

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

5条回答
  •  無奈伤痛
    2020-12-12 12:59

    You can check if the intersection is empty.

    cheeses = %w(chedder stilton brie mozzarella feta haloumi)
    foods = %w(pizza feta foods bread biscuits yoghurt bacon)
    foods & cheeses
    => ["feta"] 
    (foods & cheeses).empty?
    => false
    

提交回复
热议问题