What if no single number exists in the array


  • 0
    W

    I was asked in an interview. In the normal method, we initialized the res to the zero. But what if there is no single number exists in the array? How to know the final result is 0 or no single number?


  • 0
    I

    There's no way to know the difference, if you're returning an int in all cases. If you return an Integer, you could return null in the case where no single number exists. Or you could throw an Exception if the function expects a single number to be there.


  • 0

    @wwtwxlwjh Easy, if the array has odd length then it's a single zero and if has even length then there's no single number.


Log in to reply
 

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