• blueziocommented on "Find the unique number" java solution

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.

• jeffreson19commented on "Find the unique number" java solution

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?

• blueziocommented on "Find the unique number" java solution

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

• valeravovnovcommented on "Find the unique number" java solution

really ? hahaha, ok if massive will be 1000000?

• vinok88commented on "Find the unique number" java solution

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.

• ChickenTransolvercommented on "Find the unique number" java solution

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.

• Davionkcommented on "Find the unique number" java 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.

• mateenkasimcommented on "Find the unique number" java solution

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

• thornejoshcommented on "Backwards Read Primes" python solution

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

• Haksellcommented on "Backwards Read Primes" python solution

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

• niketkhcommented on "Duplicate Encoder" java solution

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)

• FArekkusucommented on "Find the unique number" java solution

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

• Davionkcommented on "Find the unique number" java solution

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!!!

• AlexDecommented on "Duplicate Encoder" java solution

Cool solution!

• hskarpetiscommented on "Find the unique number" java solution

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.