Skip to content

Commit 3c6ed62

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 540_Single_Element_in_a_Sorted_Array.java
1 parent 716582b commit 3c6ed62

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int singleNonDuplicate(int[] nums) {
3+
if (nums == null || nums.length == 0) {
4+
return 0;
5+
}
6+
7+
int start = 0, end = nums.length - 1;
8+
9+
while (start < end) {
10+
int mid = start + (end - start) / 2;
11+
12+
if (mid % 2 == 0) {
13+
if (nums[mid] == nums[mid + 1]) {
14+
start = mid + 1;
15+
} else {
16+
end = mid;
17+
}
18+
} else {
19+
if (nums[mid] == nums[mid - 1]) {
20+
start = mid + 1;
21+
} else {
22+
end = mid;
23+
}
24+
}
25+
}
26+
27+
return nums[start];
28+
}
29+
}

0 commit comments

Comments
 (0)