Skip to content

Instantly share code, notes, and snippets.

@craftninja
Created May 10, 2014 23:34
Show Gist options
  • Save craftninja/d5718dbba3baf7c5b97d to your computer and use it in GitHub Desktop.
Save craftninja/d5718dbba3baf7c5b97d to your computer and use it in GitHub Desktop.
data = [1,2,3,2,2,1,4,5,4,3]
result = data.reduce(Hash.new {|hash, key| hash[key] = []}) do |hash, number|
hash[number] << number
hash
end
result = {}
data.each do |number|
result[number] ||= []
result[number] << number
end
p result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment