Find values in common between two arrays

后端 未结 3 1693
北海茫月
北海茫月 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:20

    OK, so the & operator appears to be the only thing you need to do to get this answer.

    But before I knew that I wrote a quick monkey patch to the array class to do this:

    class Array
      def self.shared(a1, a2)
        utf = a1 - a2 #utf stands for 'unique to first', i.e. unique to a1 set (not in a2)
        a1 - utf
      end
    end
    

    The & operator is the correct answer here though. More elegant.

提交回复
热议问题