Problem · Array
Single Element in a Sorted Array
Learn this problemProblem statement
Given a sorted integer array nums, every value appears exactly twice except for one value that appears exactly once.
Return the value that appears once. Your solution must run in O(log n) time and use O(1) extra space.
Function
singleNonDuplicate(nums: int[]) → intExamples
Example 1
nums = [1,1,2,3,3,4,4]return = 2Every value except 2 belongs to an adjacent pair.
Example 2
nums = [0]return = 0The only element is the unique value.
Constraints
1 <= nums.length <= 100000nums.lengthis odd.-2147483648 <= nums[i] <= 2147483647numsis sorted in nondecreasing order.- Exactly one value appears once; every other value appears exactly twice.