The favorite face
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
- indexes
Problem
Every night Ivan plays a board game on his console, and he swears the game's die has a favorite number. The game keeps a record of every roll, so at last he can check.
Write a function that takes the list of rolls, each one a whole number from 1 to 6, and returns the face that came up most often, as a whole number.
With 3, 6, 3, 1, 6 and 3, the 3 came up three times, the 6 twice and the 1 once: you return 3.
If several faces tie for the most rolls, return the smallest of them: with 4, 2, 4, 2 and 6, the 4 and the 2 came up twice each, and you return 2, even though the 4 showed up first. If the list is empty, there is no favorite face and you return 0.
Examples
The example
[3, 6, 3, 1, 6, 3] → 3
The 5 wins by one
[5, 2, 5, 6, 5, 2, 1] → 5
A tie between the 4 and the 2
[4, 2, 4, 2, 6] → 2
The 6 wins
[6, 6, 1, 6] → 6
The 2 catches up with the 1 and passes it
[1, 1, 1, 2, 2, 2, 2] → 2
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def favorite_face(rolls):
passJavaScript
function favoriteFace(rolls) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.