Leetcode 540 Solution
This article provides solution to leetcode question 540 (single-element-in-a-sorted-array)
Access this page by simply typing in "lcs 540" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/single-element-in-a-sorted-array
Solution
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
l = 0
r = len(nums) - 1
while l < r:
m = (l + r) // 2
if m % 2 == 0:
if nums[m] == nums[m + 1]:
l = m + 1
else:
r = m
elif m % 2 == 1:
if nums[m] != nums[m + 1]:
l = m + 1
else:
r = m
return nums[l]