Here's my answer that works not bad


  • -1
    V

    Here's my answer that works.
    It check if nums[i] and nums[i+1] is the same.
    If not, jump into 2 alternative branches. The first one return nums[i+1] if it's the last possible. The second one check nums[i+1] and nums[i+2] is the same. (Because the last branch already prove that its not the last possible of the list, so don't have to worry about OutOfRange)
    Finally if there's no more solution, we can deduct that it's the first which is alone.

    class Solution(object):
        def singleNonDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            for i in range(len(nums) - 1):
                if nums[i] != nums[i+1]:
                    if i == len(nums) - 2:
                        return nums[i+1]
                    if nums[i+1] != nums[i+2]:
                        return nums[i+1]
            return nums[0]

Log in to reply
 

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