Binary Search based approach in Python


  • 1
    R
    class Solution(object):
        def singleNonDuplicate(self, list):
            low, high = 0 , len(list)-1
            while (low<high):
                mid = low + (high-low)/2
                if (list[mid]!=list[mid+1] and list[mid]!=list[mid-1]):
                    return list[mid]
                elif (mid%2 ==1 and list[mid]==list[mid-1]):
                    low = mid+1
                elif (mid%2 ==0 and list[mid]==list[mid+1]):
                    low = mid+1
                else:
                    high = mid-1
            return list[low]
    

Log in to reply
 

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