"30-Day Coding Challenge: Finding a Non-Duplicate in a Sorted Array"

🚀 Day 4 | 30-Day Coding Challenge – Educative.io Today’s problem: Find the single non-duplicate element in a sorted array. At first, I explored the simple O(n) approaches — 🔹 Using a Set to track duplicates (O(n) time + O(n) space) 🔹 Summing unique vs. total values and finding the difference (O(n) time) 🔹 Linear scan comparing curr and next (O(n) time, O(1) space) All worked… but the goal was O(log n) time and O(1) space. Then it clicked 💡 — since the array is sorted, every pair of equal numbers appears side-by-side. Before the unique element, pairs start at even indices; after it, they start at odd indices. Using this pattern with binary search, I halved the search space each step: if nums[mid] == nums[mid+1] → move right else move left. Every challenge so far is sharpening how I reason about patterns and constraints rather than just syntax. #Day4 #30DaysOfCode #Educative #JavaScript #BinarySearch #ProblemSolving #DSA #LearningInPublic

To view or add a comment, sign in

Explore content categories