From: "David A. Black" Date: 2009-09-26T21:01:32+09:00 Subject: Re: How to remove duplicate elements in a 2D array Hi -- On Sat, 26 Sep 2009, Thairuby ->a, b {a + b} wrote: > I think you want an array with unique member. If the member is not > unique > then return the last occur of it. > > arr=[[ 'a',1,2,3], > ['b',4,5,6], > ['c',3,2,1], > ['a',1.3,2.2,3,3], > ['a', 2,1,3], > ['a',2.1,1.5,3]] > > result = arr.inject({}){|x,y| x[y[0]]=y[1..-1]; x}.map(&:flatten) > p result > > # [["a", 2.1, 1.5, 3], ["b", 4, 5, 6], ["c", 3, 2, 1]] You could economize on intermediate objects and method calls like this: result = arr.inject({}){|x,y| x[y[0]]=y; x}.values That way, you don't take the first element out, so you don't have to put it back. David -- David A. Black, Director Ruby Power and Light, LLC (http://www.rubypal.com) Ruby/Rails training, consulting, mentoring, code review Book: The Well-Grounded Rubyist (http://www.manning.com/black2)