📌📌Python3 || ⚡166 ms, faster than 95.30% of Python3
\n\n\nclass Solution:\n    def singleNonDuplicate(self, nums: List[int]) -> int:\n        n = len(nums)\n        start = 0\n        end = n - 1\n        \n     
![image](https://assets.leetcode.com/users/images/efc9658c-14ab-4771-b4c6-9ca50950ec75_1676938125.9127867.png)\n\n```\nclass Solution:\n    def singleNonDuplicate(self, nums: List[int]) -> int:\n        n = len(nums)\n        start = 0\n        end = n - 1\n        \n        while start <= end:\n            mid = start + (end - start) // 2\n            a = mid % 2 == 0 and nums[mid] == nums[mid - 1]\n            b = mid % 2 != 0 and nums[mid] == nums[mid + 1]\n            if a or b:\n                end = mid - 1\n            else: \n                start = mid + 1\n        return nums[start - 1]\n```\n\nThe code is implementing the binary search algorithm to find the single element in a sorted array which appears only once. Here\'s a step by step explanation of the code:\n1. First, the length of the input array is stored in the variable \'n\'.\n1. Then, the start and end indices of the array are set to 0 and n - 1 respectively.\n1. A while loop is initiated that continues while the start index is less than or equal to the end index.\n1. Inside the while loop, the mid index is calculated using the formula "start + (end - start) // 2". This is equivalent to (start + end) // 2 but avoids potential integer overflow issues.\n1. Two boolean variables \'a\' and \'b\' are created to check if the mid index is pointing to the single element. \'a\' is True if the mid index is even and the element at mid is equal to the element at mid - 1. \'b\' is True if the mid index is odd and the element at mid is equal to the element at mid + 1.\n1. If either \'a\' or \'b\' is True, then it means that the single element is in the left half of the array. Therefore, the end index is updated to mid - 1.\n1. If \'a\' and \'b\' are both False, then the single element is in the right half of the array. Therefore, the start index is updated to mid + 1.\n1. After the while loop completes, the function returns the element at index start - 1. This is because the start index will always point to the first occurrence of the single element, and decrementing it by 1 gives the index of the single element.
['Binary Search', 'Binary Tree', 'Python', 'Python3']