Skip to content

Instantly share code, notes, and snippets.

@kangkyu
Last active August 29, 2015 14:27
Show Gist options
  • Save kangkyu/f394a2cc4b8ae9902093 to your computer and use it in GitHub Desktop.
Save kangkyu/f394a2cc4b8ae9902093 to your computer and use it in GitHub Desktop.
# return the unique elements of this array:
# a = []
# 10000000.times{a.push(rand(100))}
# no array.uniq please
# extra credit -- in one function, count all the occurrences of each element of a
a = []
10000000.times{a.push(rand(100))}
b = []
count_hash = Hash.new(0)
while a.count > 0 do
pick = a.sample
count_hash[pick] = a.count(pick)
b.push a.delete pick
end
b.inspect
count_hash.inspect
@charliemcelfresh
Copy link

excellent! I was going for the use of Set -- but this is excellent.
s = Set.new
a.map{|e| Set.add(e)}

Set is the great unused class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment