aboutsummaryrefslogtreecommitdiff
path: root/challenge-284/steven-wilson/python/ch-1.py
blob: c0d62d056fe57091d258986bffe27dafaa7fa9b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python3

from collections import Counter


def lucky_integer(*integers):
    """ Given an array of integers, return the lucky integer if found otherwise
    return -1. If there are more than one then return the largest.

        A lucky integer is an integer that has a frequency in the array equal
        to its value.

    >>> lucky_integer(2, 2, 3, 4)
    2
    >>> lucky_integer(1, 2, 2, 3, 3, 3)
    3
    >>> lucky_integer(1, 1, 1, 3)
    -1
    """
    counter = Counter(integers)
    return max((i for i, v in counter.items() if i == v), default=-1)


if __name__ == "__main__":
    import doctest

    doctest.testmod(verbose=True)