Skip to content

Commit

Permalink
feat: LC-0540: Single Element in a Sorted Array
Browse files Browse the repository at this point in the history
add java solution for leetcode problem 540 - Single Element in a Sorted Array
  • Loading branch information
galaumang committed Oct 3, 2023
1 parent 4f172e8 commit 2206560
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dsalgo.array;

public class LC0540_SingleElementSortedArray {
public int singleNonDuplicate(int[] nums) {
int n = nums.length;
if (n == 1)
return nums[0];

for (int i = 0, j = 1; i < n - 1; i += 2) {
if (nums[i] != nums[j]) {
return nums[i];
}
j += 2;
}
return nums[n - 1];
}
}

0 comments on commit 2206560

Please sign in to comment.