{"id":126,"date":"2010-02-19T16:41:03","date_gmt":"2010-02-19T22:41:03","guid":{"rendered":"http:\/\/benincosa.com\/blog\/?p=126"},"modified":"2014-11-19T11:26:55","modified_gmt":"2014-11-19T17:26:55","slug":"ruby-on-rails-intersections-of-multiple-arrays","status":"publish","type":"post","link":"https:\/\/benincosa.com\/?p=126","title":{"rendered":"Ruby on Rails intersections of multiple arrays"},"content":{"rendered":"<p>finding the intersection in Ruby of 2 arrays is quite easy.  If a is an array and b is an array then c can be the intersection as follows:<br \/>\nc = a &amp; b<br \/>\nSimilarly the union of the arrays can be found as follows:<br \/>\nc = a | b<br \/>\nThe problem I had was finding the intersection of multiple arrays.  So how to do it?<br \/>\nI did it as follows:<br \/>\n1.  First find the union of all arrays.<br \/>\n2.  Then take the intersection of the big union with each of the individual parts.<\/p>\n<p>This sounds vaguely familiar to me as perhaps it was something I did in my computer science class at one point.<br \/>\nAnyway, here is the function:<\/p>\n<pre>  def intersect(array_of_arrays)\r\n    ar = []\r\n    # first find all the commons, then do intersection on it.\r\n    array_of_arrays.each  { |a|\r\n      ar = a| ar\r\n    }\r\n    # now take the intersection of each array  with the union of them all\r\n    array_of_arrays.each  { |a|\r\n      ar = a &amp; ar\r\n    }\r\n    ar\r\n  end<\/pre>\n<p>Anyway, if there&#8217;s some better way to do it, I&#8217;d love to know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>finding the intersection in Ruby of 2 arrays is quite easy. If a is an array and b is an array then c can be the intersection as follows: c = a &amp; b Similarly the union of the arrays can be found as follows: c = a | b The problem I had was&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[50,983],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/126"}],"collection":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=126"}],"version-history":[{"count":3,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":2830,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions\/2830"}],"wp:attachment":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}