From: Jimmy Kofler Date: 2007-08-22T04:42:07+09:00 Subject: Re: Detecting duplicates in an array, anything in the standa > Jeremy Woertink wrote: > I actually had to ... find all the duplicate account > numbers and the number of times they were duplicated and ... . > ... > ~Jeremy A much less verbose 'nil' fix of the original version would be to use [v] instead of v: a = [nil,1,2,2,3,nil] p a.uniq.map {|v| (a - [v]).size < (a.size - 1) ? [v] : nil}.compact.flatten => [nil, 2] And with this fixed version it's also possible to count & grab duplicate array items in one go: a = [nil,1,2,2,3,nil,nil] a = (a * 5 << "unique_obj1" << "unique_obj2").sort_by { rand } p a.uniq.map {|v| diff = (a.size - (a-[v]).size); (diff > 1) ? [v, diff] : nil}.compact => [[2, 10], [3, 5], [nil, 15], [1, 5]] Cheers, j.k. -- Posted via http://www.ruby-forum.com/.