We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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 :
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 !
Migratory Birds
You are viewing a single comment's thread. Return to all 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 :
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