On Jan 12, 2008, at 9:24 AM, Bil Kleb wrote:

> Given unused is a hash of arrays where each array has
> repeated elements, make elements unique in each array and
> remove all names that are not repeated MagicNumber times.
> Finally, remove any hash pairs that hold an empty array.
>
> unused.each do |source,routines|
>  orphans = routines.uniq
>  orphans.delete_if do |orphan|
>    repeated = routines.find_all{ |routine| routine == orphan }.size
>    repeated != MagicNumber
>  end
>  unused[source] = orphans
> end
> unused.delete_if{ |h,k| k.empty? }

unused = unused.inject(Hash.new) |result, (source, routines)|
   orphans = routines.uniq.reject do |o|
     routines.inject(0) { |c, r| c + (r == o ? 1 : 0) } != MagicNumber
   end
   orphans.empty? ? result : result.merge(source => orphans)
end

Hmm, I'm not really sure I did much better there.

James Edward Gray II