Single Element in a Sorted Array
Problem Descriptionβ
Problem Statement | Solution Link | LeetCode Profile |
---|---|---|
[Single Element in a Sorted Array](https://leetcode.com/problems/Single Element in a Sorted Array/description/) | Single Element in a Sorted Array Solution on LeetCode | Nikita Saini |
Problem Descriptionβ
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.
Examplesβ
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
Constraintsβ
Approachβ
To solve this problem in time complexity and space complexity, we can use a binary search approach. The key observation is that if we split the array into pairs, the single element will disrupt the pairing. By checking the indices, we can determine which half of the array to search next.
Step-by-Step Algorithmβ
- Initialize
left
to 0 andright
tolen(nums) - 1
. - Use a binary search loop:
while left < right
:- Calculate the middle index
mid = left + (right - left) // 2
. - Check if
mid
is even. If true, check ifnums[mid]
is equal tonums[mid + 1]
.- If they are equal, the single element is in the right half, so set
left = mid + 2
. - Otherwise, the single element is in the left half, so set
right = mid
.
- If they are equal, the single element is in the right half, so set
- If
mid
is odd, check ifnums[mid]
is equal tonums[mid - 1]
.- If they are equal, the single element is in the right half, so set
left = mid + 1
. - Otherwise, the single element is in the left half, so set
right = mid - 1
.
- If they are equal, the single element is in the right half, so set
- Calculate the middle index
- When the loop exits,
left
will be at the single element.
Solution in Pythonβ
def singleNonDuplicate(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = left + (right - left) // 2
if mid % 2 == 0:
if nums[mid] == nums[mid + 1]:
left = mid + 2
else:
right = mid
else:
if nums[mid] == nums[mid - 1]:
left = mid + 1
else:
right = mid - 1
return nums[left]
Solution in Javaβ
public class Solution {
public int singleNonDuplicate(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (mid % 2 == 0) {
if (nums[mid] == nums[mid + 1]) {
left = mid + 2;
} else {
right = mid;
}
} else {
if (nums[mid] == nums[mid - 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return nums[left];
}
}
Solution in C++β
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (mid % 2 == 0) {
if (nums[mid] == nums[mid + 1]) {
left = mid + 2;
} else {
right = mid;
}
} else {
if (nums[mid] == nums[mid - 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return nums[left];
}
};
Solution in Cβ
int singleNonDuplicate(int* nums, int numsSize){
int left = 0, right = numsSize - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (mid % 2 == 0) {
if (nums[mid] == nums[mid + 1]) {
left = mid + 2;
} else {
right = mid;
}
} else {
if (nums[mid] == nums[mid - 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return nums[left];
}
Solution in JavaScriptβ
function singleNonDuplicate(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
const mid = left + Math.floor((right - left) / 2);
if (mid % 2 === 0) {
if (nums[mid] === nums[mid + 1]) {
left = mid + 2;
} else {
right = mid;
}
} else {
if (nums[mid] === nums[mid - 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return nums[left];
}
Conclusionβ
By leveraging binary search, we can efficiently find the single element that appears only once in a sorted array where every other element appears twice. This approach ensures an O(log n) time complexity and O(1) space complexity, making it an optimal solution for this problem.