# Single Element in a Sorted Array (Leetcode 540)

Problem Link: <https://leetcode.com/problems/single-element-in-a-sorted-array/>

```python
class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
        
        def modifiedbsearch(nums,i,j,n):
            
            # Check the corner cases :
            # if mid==0 and mid==n-1
            mid=(i+j)//2
            while(i<=j):
                if(mid==0):
                    if(nums[mid]!=nums[mid+1]):
                        return mid
                    else:
                        return modifiedbsearch(nums,mid+1,j,n)
                elif(mid==n-1):
                    if(nums[mid]!=nums[mid-1]):
                        return mid
                    else:
                        return modifiedbsearch(nums,i,mid-1,n)
                else:
                    if(nums[mid]!=nums[mid-1] and nums[mid]!=nums[mid+1]):
                        return mid
                    elif(mid%2==0):
                        if(nums[mid]==nums[mid+1]):
                            return modifiedbsearch(nums,mid+1,j,n)
                        else:
                            return modifiedbsearch(nums,i,mid-1,n)
                    else:
                        if(nums[mid]==nums[mid+1]):
                            return modifiedbsearch(nums,i,mid-1,n)
                        else:
                            return modifiedbsearch(nums,mid+1,j,n)
    
        
        n=len(nums)
        if(n==1):
            return nums[0]
        i=0
        # j=n-2 
        j=n-1
        idx=modifiedbsearch(nums,i,j,n)
        return nums[idx]
```
