• That "return -1" in my solution is really just a last ditch effort to return something. If the array meets the specified condition of having exactly one value which is different, then it'll always end up returning that one value.

    The key thing is what happens if someone used the function with the wrong array, e.g. an array with only zeros. In that case, the function would give you -1 rather than something else, but what it really should be doing is probably throwing an exception saying that the array doesn't have any different values in it.

    So thinking again about it, I'd probably change my solution to have it throw an IllegalArgumentException complaining that the array doesn't have that one different value. Then either you get a value if the array meets the expected condition of having exactly one different value, or you get an exception complaining that you gave it a bad array.

  • ammm i saw your solution but what if there are an array with -1 as the unique number. you would have -1 either ways so how would you know if the -1 represent no unique number or actually the unique number?

  • You don't need to sort - that's O(n log n) at best. A single linear scan will give you the unique number.

  • really ? hahaha, ok if massive will be 1000000?

  • This is a bad solution, it only looks nice. performance wise this is below PAR. Why the hell we need to sort while we can go through the array only once up untill we get the unique.

  • After i spent a while programming my own solution, I am astonished about these two lines. I think this is near to the optimal solution.

  • It is not actually. It is more my stupid mistake for saying that in the end of my comment ;P. As you can see the first reply to my comment explains that I am wrong in a funny way :D.

    Again sorry for the last part of my comment and thanks for the reply.

  • How is this O(n/2) if you're still doing ~n comparisons? The loop runs n/2 times but you do 2 comparisons per loop. O(n) is still great though, better than sorting

  • This comment is hidden because it contains spoiler information about the solution

  • There is no need to join str(n)[::-1], since it is already a string.

  • I agree this is a cool solution but far from following best practices. Runtime complexity for this solution is O(N^2) which can easily be solved in O(N)

  • tecnically speaking it was O(n / 2)

    You could improve it drastically by splitting the array into n 1-element-long segments, and have O(n / n) = O(1) time complexity :+1:

  • This is an ok solution. But that's all.
    It is the same as all standard solutions, just more nice to the eye.

    The kata specifies to give emphasis to efficiency!

    Go check my solution, I solved it in O(N). Even though tecnically speaking it was O(N/2), so half the time!!!

  • It's very very slow practice.
    Only two lines of code , BUT it isn't clever practise.
    It is worse than loop all the elements of the array.