From: "Ara.T.Howard" Date: 2005-10-16T23:11:56+09:00 Subject: Re: How to get non-unique elements from an array? On Sun, 16 Oct 2005, Robert Klemme wrote: > para.hsu@gmail.com wrote: >> Hello! >> >> How about this ? >> (0...a.length-1).select{|i| a[i]==a[i+1]}.map{|i|a[i]}.uniq > > a needs to be sorted for this to work. As far as I remember that was not a > guaranteed precondition so you would have to add that here. > > IMHO most efficient variants are still those that use a hash for element > counting. and you needed even count i think: harp:~ > cat a.rb class Array def dups h, d = {}, []; each{|e| h[e] ? (d << h.delete(e)) : (h[e] = e) }; d end end a = 0, 1, 2, 3, 4, 5, 2, 3, 2 p a.dups harp:~ > ruby a.rb [2, 3] regards. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | anything that contradicts experience and logic should be abandoned. | -- h.h. the 14th dalai lama ===============================================================================