From: Gavin Kistner Date: 2006-10-13T04:55:07+09:00 Subject: Re: Isolating non-unique items in an array > From: Phrogz [mailto:gavin@refinery.com] > class Array > def duplicates_count > uniq.map{ |e| > if ( count = grep(e).size ) > 1 > { e => count } > end > }.compact > end > end > > a = ["a", "a", "a", "b", "c", "d", "d"] > p a.duplicates_count > #=> [{"a"=>3}, {"d"=>2}] Don't listen to that Phrogz character, he's insane! More like: class Array def duplicates_count Hash[ *uniq.map{ |e| if ( count = grep(e).size ) > 1 [e, count] end }.compact.flatten ] end end a = ["a", "a", "a", "b", "c", "d", "d"] p a.duplicates_count #=> {"a"=>3, "d"=>2} But of course, other posts in this thread are likely faster.