Description specifies two different rules for which duplicate to return. The second is wrong.
Returning -1 is not a good choice. It might be a valid return value as well ( you never specify that the array only contains nonnegative numbers ). undefined or in this case maybe NaN (!) might have been better choices, if you don't want to throw an Error.
map[] === true ( in the reference solution ) is a bit of an antipattern, isn't it? It's a Boolean already.
This comment is hidden because it contains spoiler information about the solution
You can bake a basic cake is using the following ingredients
Corrected:
You can bake a basic cake by using the following ingredients
A classical question for an interview job and very important to know it. Unfortunately it's a duplicate of a kata previously published: https://www.codewars.com/kata/find-the-duplicated-number-in-a-consecutive-unsorted-list-tougher-version
And many others with very similar.
Description specifies two different rules for which duplicate to return. The second is wrong.
Returning
-1
is not a good choice. It might be a valid return value as well ( you never specify that the array only contains nonnegative numbers ).undefined
or in this case maybeNaN
(!) might have been better choices, if you don't want to throw anError
.map[] === true
( in the reference solution ) is a bit of an antipattern, isn't it? It's aBoolean
already.