Skip to content

Instantly share code, notes, and snippets.

@pinkopaque22
Last active March 20, 2016 19:25
Show Gist options
  • Save pinkopaque22/0d02b06d9009e6752a7d to your computer and use it in GitHub Desktop.
Save pinkopaque22/0d02b06d9009e6752a7d to your computer and use it in GitHub Desktop.
Solution for Codewars Most Frequent Item Count in Python
//Codewars Kata Level 7 (easy)//
MOST FREQUENT ITEM COUNT
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 occures 5 times in input array.
Test Cases
1.) Test.describe("most_frequent_item_count")
2.) Test.it("works for some examples")
3.) Test.assert_equals(most_frequent_item_count([3, -1, -1]), 2, "didn't work for [3, -1, -1]")
4.) Test.assert_equals(most_frequent_item_count([3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3]), 5, "didn't work for [3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3]")
5.) Test.assert_equals(most_frequent_item_count([]), 0, "didn't work for []")
6.) Test.assert_equals(most_frequent_item_count([9]), 1, "didn't work for [9]")
//////////CODE//////////////
#from collections import Counter would implement portions of the function below in a better way but I don't know how to use them.
def most_frequent_item_count(collection):
if len(collection) == 0:
return 0
my_max_count = collection.count(collection[0])
my_max_value = collection[0]
for item in collection:
if collection.count(item) > my_max_count:
my_max_count = collection.count(item)
my_max_value = item
return my_max_count
@pinkopaque22
Copy link
Author

Resources:
Reddit
Stack Overflow

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