On Thu, Jun 21, 2001 at 02:56:51AM +0900, Hugh Sasse Staff Elec Eng wrote: > Is there an elegant way to do a bag diffeence between 2 arrays, rather > than a set difference? > > [1,1,1,2,3,3,4,4,4] <something> [1,2,3,4,4] gives: > [1,1,3,4] > > If ordering is preserved that would be great. Don't know if this is the most elegant solution, but it is the first that comes to my mind. class Array def with_count h = {}; h.default = 0 self.collect {|x| [x, h[x] += 1]} end def bagminus(other) (self.with_count - other.with_count).collect {|x| x[0]} end end a = [1,1,1,2,3,3,4,4,4] b = [1,2,3,4,4] p a.bagminus b # --> [1, 1, 3, 4] // Niklas