Single Element in a Sorted Array
Updated
Problem statement
You are given an integer array nums sorted in non-decreasing order. Every value in nums appears exactly twice, except for one value that appears exactly once.
Return the value that appears exactly once.
Your solution should run in O(log n) time and use O(1) extra space, where n is the length of nums.
Function
singleNonDuplicate(nums: int[]) → intExamples
Example 1
nums = [1,1,4,4,6,8,8]return = 61, 4 and 8 each appear twice. 6 appears once.
Example 2
nums = [3,5,5,9,9]return = 3The single value can be the first element. 5 and 9 each appear twice after it.
Example 3
nums = [2,2,4,4,7]return = 7The single value can also be the last element.
Constraints
1 <= nums.length <= 10^50 <= nums[i] <= 10^5numsis sorted in non-decreasing order.- Exactly one value appears once; every other value appears exactly twice.
More GreyOrange interview questions
Similar questions at other companies
- Count Winning Dice Face PairsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Search in a Rotated Sorted ArrayAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
- Koko Eating BananasThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Find First and Last Position in a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Longest Increasing SubsequenceAngel One · PHONE SCREEN
- Search a 2D MatrixWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Find First and Last Position of Element in Sorted ArrayEPAM · PHONE SCREENEPAM interview questions
- Find K Closest Elements in a Sorted ArrayNavi · PHONE SCREEN
- Search in Rotated Sorted ArrayZoomInfo · PHONE SCREEN