Does the array have to be sorted?


  • 0
    I

    After coming up with a binary search solution myself and having seen some very elegant solutions here (my goodness those are genius!), I wonder what kind of role the sorted property played in this problem.

    It seems the array doesn't have to be sorted. If you look at the code: they are not comparing elements a normal binary search does, there's no greater/less than conditions at all. And I tested the code with unsorted arrays, keeping the duplicate elements together of course, and it seems to work.

    So it seems the only reason by stating the array is sorted is to simplify the problem statement: so that instead of saying array with elements appearing twice except one, and the duplicate elements always stick together; saying it's sorted seems shorten the description a little.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.