From: "David A. Black" Date: 2009-09-26T06:00:35+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]] Another way, at least in 1.9: result = Hash[arr.map {|a| [a.first, a] }].values 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)