From: Rick DeNatale Date: 2007-10-13T03:32:29+09:00 Subject: Re: Finding shared elements between to arrays. On 10/11/07, Eric I. wrote: > Well a1 - (a1 - a2) is not necessarily the same as a2 - (a2 - a1). > The difference is whether the duplicates in a1 are maintained (the > former) or the duplicates is a2 are maintained. > > For example, suppose a1 is [1, 1, 2, 3, 4] and a2 is [1, 2, 2, 3, 5]. > The result would be either [1, 1, 2, 3] or [1, 2, 2, 3]. Yep, you're right, not enough test cases! I realize now that the OPs request for "an array which has the elements shared by two other arrays" is a little ambiguous as to duplications. Note that the OPs solutions and the solutions using select produce the same different results if you reverse the two arrays. a1 = [1,1,2,3,4] a2 = [1,2,2,3,5] a1 - (a1-a2) => [1, 1, 2, 3] a2 - (a2-a1)=> [1, 2, 2, 3] a1.select {|e| a2.include? e} => [1, 1, 2, 3] a2.select {|e| a1.include? e}=> [1, 2, 2, 3] Should the elements shared between [1,1,2,3,4] and [1,2,2,3,5] be different than the elements shared between [1,2,2,3,5] and [1,1,2,3,4]? -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/