• + 106 comments

    Since a lot of codes shared are complicated as well as sub-optimal. Here's a simple, concise and optimal approach for coders looking for an elegant solution :

    input()
    count = [0]*6
    for t in map(int,input().strip().split()):
        count[t] += 1
    print(count.index(max(count)))
    

    The solution is and traverses the given list only once.


    For aspiring programmers :

    Be careful while learning from the codes you read. I haven't checked for all languages, but a lot of python-codes here, are even incorrect (including the editorial as well as currently topvoted comment). Particularly for this problem, be cautious if you see usage of "set", "dictionary" or "Counter" anywhere.

    Dictionaries/sets are unordered in python, which means one cannot trust the outcome where order matters. In fact, these solutions would always give wrong result for simple testcase like 2, 2, 1, 1 in Python 3.6. Thus, as programmers we need to ensure that our code succeeds always, and not only by chance !


    Informative Tweets for Inquisitive Minds