Created
January 7, 2018 22:36
-
-
Save RickGriff/9779ea24385721a923e08f4b0aef736f to your computer and use it in GitHub Desktop.
Codewars Challenge: Find Count of Most Frequent Item 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
# Write a program to find count of the most frequent item of an array. | |
# Assume that input is array of integers. | |
# Ex.: | |
# input array: [3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3] | |
# ouptut: 5 | |
# Most frequent number in example array is -1. It occurs 5 times in input array. | |
----- | |
#My Solution: | |
def most_frequent_item_count(collection) | |
return 0 if collection == [] | |
hist = collection.each_with_object(Hash.new(0)){ |num, freq| freq[num] += 1 } | |
hist.values.max | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment