From: Li Chen Date: 2009-09-26T05:50:36+09:00 Subject: Re: How to remove duplicate elements in a 2D array Yes this is what I want. But can you explain the code line in a little bit more? Thanks, Li 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]] -- Posted via http://www.ruby-forum.com/.