From: ara.t.howard@... Date: 2006-10-13T05:53:27+09:00 Subject: Re: Isolating non-unique items in an array On Fri, 13 Oct 2006, Phrogz wrote: > WATANABE Hirofumi wrote: >> Jason Burgett writes: >>> ["a", "a", "a", "d", "d"] >>> >>> Then I need to somehow determine that "a" appears 3 times and "d" >>> appears 2 times. Any help would be great. Thanks. >> >> % irb >>>> a = ["a", "a", "a", "b", "c", "d", "d"] >> => ["a", "a", "a", "b", "c", "d", "d"] >>>> a.select{|i| a.grep(i).size > 1} >> => ["a", "a", "a", "d", "d"] > > Taking that one step further to meet his second need: > > 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}] that's O(n^2) though... the others are O(n) -a -- my religion is very simple. my religion is kindness. -- the dalai lama