Skip to content

Instantly share code, notes, and snippets.

@RickGriff
Created January 7, 2018 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickGriff/9779ea24385721a923e08f4b0aef736f to your computer and use it in GitHub Desktop.
Save RickGriff/9779ea24385721a923e08f4b0aef736f to your computer and use it in GitHub Desktop.
Codewars Challenge: Find Count of Most Frequent Item in Array
# 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