Find values in common between two arrays

后端 未结 3 1666
北海茫月
北海茫月 2021-01-30 10:52

If I want to compare two arrays and create an interpolated output string if an array variable from array y exists in x how can I get an output for each

3条回答
  •  伪装坚强ぢ
    2021-01-30 11:09

    x = [1, 2, 4]
    y = [5, 2, 4]
    intersection = (x & y)
    num = intersection.length
    puts "There are #{num} numbers common in both arrays. Numbers are #{intersection}"
    

    Will output:

    There are 2 numbers common in both arrays. Numbers are [2, 4]
    

提交回复
热议问题