Last active
August 29, 2015 14:27
-
-
Save kangkyu/f394a2cc4b8ae9902093 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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