From: Harry Kakueki Date: 2009-09-27T00:42:58+09:00 Subject: Re: How to remove duplicate elements in a 2D array On Sat, Sep 26, 2009 at 12:34 AM, Li Chen wrote: > Hi all, > > I have a 2D array as follow: > > 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] > > ] > > As you can see there are several rows containing element 'a', > and the last row containing 'a'' is the lastest one. I wonder if it > is possible to return a new array which contains the latest row having > 'a' , > together with other rows. > > > Thanks, > > Li > -- > Posted via http://www.ruby-forum.com/. > > Here is another way. It works with Ruby 1.8.6. p arr.map{|x| x[0]}.uniq.map{|y| arr.reverse.detect{|z| z[0] == y}} # OR if order is important, # p arr.reverse.map{|x| x[0]}.uniq.reverse.map{|y| arr.reverse.detect{|z| z[0] == y}} Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/ruby/list.html