Most common
Inputs
values is a list of 1 to 14 whole numbers, each from 0 to 6.
Your tasks
- Create a list
countsof 7 zeros. Use a loop overvaluesand add 1 atcounts[value]for each number. - Use a second loop over
countsto find the number with the highest count. - Complete
mostCommonso it returns the number that appears most often. Return it, do not print it.
Example
With values = [3, 1, 3, 2, 1, 3, 1], mostCommon returns 1.
Rules
- If several numbers tie, return the smallest.
valuesalways has at least one number.
Test data
TEST DATA 1: values is [3, 1, 3, 2, 1, 3, 1]
TEST DATA 2: values is [5]
Practice focus
Prefix sums, counting, binary search, best stretch and sorting, all written as plain loops. This is where loops turn into named algorithms. You have already written the loops.