From: 7stud -- Date: 2009-08-10T08:17:42+09:00 Subject: Re: How to define doubles: Array-to-hash Bertram Scharpf wrote: > Here are two: > > original.inject({}) { |h,e| h[e] ||= 0 ; h[e] += 1 ; h } > > and > > h = Hash.new { |h,k| h[k] = 0 } > original.each { |e| h[e] += 1 } > h > > then > > h.reject! { |k,v| k == 1 } > > Bertram Your code doesn't produce the desired result, the op wanted an efficient solution, and this: h = Hash.new { |h,k| h[k] = 0 } is equivalent to: h = Hash.new(0) How about a regex? original = [1, 2, 3, 3, 3, 3, 4, 4, 5] results = {} str = original.to_s str.scan(/((\d)\2+)/).each do |match| results[match[1]] = match[0].length end p results --output:-- {"3"=>4, "4"=>2} ...but that is no faster than: results = Hash.new(0) original.each do |elmt| results[elmt.to_s] += 1 end results.delete_if {|key, val| val == 1} p results --output:-- {"3"=>4, "4"=>2} -- Posted via http://www.ruby-forum.com/.