From: Siep Korteling Date: 2009-07-20T07:07:22+09:00 Subject: Re: removing array duplicates where a subset is unique David A. Black wrote: > Hi -- > > On Mon, 20 Jul 2009, 7stud -- wrote: > >>> >> [2, 2, 3, 40] >> h[key] = suba >> end >> >> end >> >> p h.values >> >> --output:-- >> [[2, 2, 3, 40], [1, 2, 3, 10]] > > I'm not sure what that buys you, though. The ||= idiom should work > fine, unless you need to do something extra during the if statement. > > > David Facets has enumerable#uniq_by implemented like this: def uniq_by #:yield: h = {}; inject([]) {|a,x| h[yield(x)] ||= a << x} end So you can do: require 'facets' a = [[1, 2, 3, 4, 5], [1, 2, 3, 9, 4], [1, 2, 3, 4, 4]] res = a.uniq_by{|el| el[0..2]} Don't know about performance. hth, Siep -- Posted via http://www.ruby-forum.com/.