Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions SingleElementInSortedArray/SingleElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
public class SingleElement {
public int singleNonDuplicate(int[] nums) {
int n = nums.length;

// Case 1: Only one element in array
if (n == 1) {
return nums[0];
}

// Case 2: Unique element is at the beginning
if (nums[0] != nums[1]) {
return nums[0];
}

// Case 3: Unique element is at the end
if (nums[n - 1] != nums[n - 2]) {
return nums[n - 1];
}

// Binary search in between
int start = 1;
int end = n - 2;
while (start <= end) {
int mid = start + (end - start) / 2;

// If nums[mid] is the unique element
if (nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) {
return nums[mid];
}

// If mid is even and matches with next OR mid is odd and matches with previous,
// then unique element lies on the right half
if ((mid % 2 == 0 && nums[mid] == nums[mid + 1]) ||
(mid % 2 == 1 && nums[mid] == nums[mid - 1])) {
start = mid + 1;
}
// Otherwise, it lies on the left half
else {
end = mid - 1;
}
}
return -1; // This case won't occur as per problem constraints
}

// Main method to test
public static void main(String[] args) {
Solution sol = new Solution();
int[] nums1 = {1, 1, 2, 3, 3, 4, 4, 8, 8};
int[] nums2 = {3, 3, 7, 7, 10, 11, 11};
int[] nums3 = {5};

System.out.println("Unique element in nums1: " + sol.singleNonDuplicate(nums1)); // Output: 2
System.out.println("Unique element in nums2: " + sol.singleNonDuplicate(nums2)); // Output: 10
System.out.println("Unique element in nums3: " + sol.singleNonDuplicate(nums3)); // Output: 5
}
}
40 changes: 40 additions & 0 deletions SingleElementInSortedArray/intuition_algo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Single Element in a Sorted Array

## 🧠 Intuition
We are given a **sorted array** where every element appears exactly **twice**, except for **one element** that appears only once.

Key observations:
1. Since the array is sorted, duplicate elements will always appear **in pairs**.
2. Before the unique element, the **first element of the pair** will appear at an **even index**, and the **second element** will appear at an **odd index**.
- Example: `[1, 1, 2, 2, 3, ...]` → `(0,1), (2,3), ...`
3. After the unique element, this pattern **breaks**.
- Example: `[1, 1, 2, 3, 3, ...]` → At index 2 (even), `2` does **not** have its pair next.

Using this property, we can apply **binary search** to efficiently find the single element in **O(log n)** time.

---

## ⚙️ Algorithm

1. **Handle edge cases**:
- If the array has only **one element**, return it.
- If the unique element is at the **start** or **end**, check the first/last two elements.

2. **Binary Search**:
- Initialize `start = 1`, `end = n-2`.
- While `start <= end`:
- Find `mid = (start + end) / 2`.
- If `nums[mid]` is **not equal** to both neighbors, it’s the unique element.
- Otherwise, check parity (`mid % 2`) to decide the search direction:
- If `(mid is even and nums[mid] == nums[mid+1])` OR `(mid is odd and nums[mid] == nums[mid-1])`, then the unique element lies on the **right half** → `start = mid + 1`.
- Else, search in the **left half** → `end = mid - 1`.

3. Return the found element.

---

## ⏱️ Time and Space Complexity
- **Time Complexity**: `O(log n)` (binary search)
- **Space Complexity**: `O(1)` (constant extra space)

---