Skip to the content

The winning number

Problem

At the school fair, Leticia sells the raffle tickets and each person picks the number they want, from 0 to 9. The prize does not go to the number that was asked for the most: it goes to the one that exactly times people asked for, not one more and not one less.

Write a function that takes numbers, the list of the numbers people asked for, in that order (it may come empty), and times, a whole number from 1 up. Return the number that appears exactly times in the list; if several of them qualify, the one that was asked for first. If none qualifies, there is no winner and you return -1.

With the numbers 7, 3, 7, 9, 3, 7 and times equal to 2, three people asked for the 7 and only one for the 9: the only one that two people asked for is the 3, so you return 3.

With the numbers 9, 4, 4, 9, 7 and times equal to 2, the 9 and the 4 both qualify, and you return 9 because it was asked for first, even though it is the bigger one.

Examples

  • The example

    [7, 3, 7, 9, 3, 7], 2 → 3

  • Everyone picked a different number

    [0, 1, 2, 3, 4, 5], 1 → 0

  • Two people picked the 1

    [1, 2, 1, 3, 4], 2 → 1

  • A longer list

    [2, 3, 4, 3, 5, 7, 1, 2, 3, 5], 2 → 2

  • Nobody qualifies

    [1, 1, 2, 2], 3 → -1

  • The winner is not the smallest

    [9, 4, 4, 9, 7], 2 → 9

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def winner(numbers, times):
    pass

JavaScript

function winner(numbers, times) {
}
Solve this challenge

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.

More O(n) challenges

See all challenges →