Last active
August 29, 2015 14:27
-
-
Save joegiralt/6ff35a21ea810e27a607 to your computer and use it in GitHub Desktop.
count duplicate items in array ()
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
array = ['jim','jack','joe','larry','larry','larry','larry','larry', 'joe'] | |
def count_items(array) | |
hash = {} | |
array.each do |name| | |
if hash[name] | |
hash[name] = hash[name] + 1 | |
else | |
hash[name] = 1 | |
end | |
end | |
puts hash | |
end | |
count_items(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment