You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

Return the single element that appears only once.

Your solution must run in O(log n) time and O(1) space.

&nbsp;
Example 1:
Input: nums = [1,1,2,3,3,4,4,8,8]
Output: 2
Example 2:
Input: nums = [3,3,7,7,10,11,11]
Output: 10

&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	0 &lt;= nums[i] &lt;= 105


class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
        return self.b_search(nums)[0]
    
    def b_search(self, nums):
        if len(nums) == 1:
            return nums
        mid = len(nums)//2
        a = nums[:mid]
        b = nums[mid:]
		
		# check if last & first element of the two sub lists are same
        if a[-1] == b[0]:
            a = a[:-1]
            b = b[1:]
		
		# ignore the sub list with even number of elements
        if len(a)%2:
            return self.b_search(a)
        else:
            return self.b_search(b)
class Solution {
    public int singleNonDuplicate(int[] nums) {
        if(nums.length==1) return nums[0];
        int l = 0;
        int h = nums.length-1;
        
        while(l<h){
            int mid = l+(h-l)/2;      // divide the array
            
            if(nums[mid]==nums[mid+1]) mid = mid-1;      //two same elements should be in same half
            
            if((mid-l+1)%2!=0) h = mid;            // checking the length of left half. If its is odd then update ur right pointer to mid
            
            else l = mid+1;    // else your right half will be odd then update your left pointer to mid+1
        }
        
        return nums[l];     //left pointer will have the answer at last
    }
}
class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        int ans=0;
        for(int i=0; i<nums.size();i++){
            ans=ans^nums[i];
        }
        return ans;
    }
};
var singleNonDuplicate = function(nums) {
    var start = 0;
    var end = nums.length - 1
    // to check one element
     if (nums.length == 1) return nums[start]
     while(start <= end) {
        if(nums[start] != nums[start + 1]) {
            return nums[start] }

          if(nums[end] != nums[end - 1]) {
            return nums[end]
        }
        // increment two point
        start = start + 2;
        end = end - 2;

    }
};
Single Element in a Sorted Array