Introduction
The Single Element in a Sorted Array problem is a popular DSA interview question that tests your understanding of binary search.
In simple terms, this problem asks you to find the only number that appears once, while all other numbers appear exactly twice.
This article explains the solution using Binary Search, written in simple, human-friendly language, with real-world meaning and clear step-by-step logic.
Real-World Meaning of Single Element
Imagine a sock drawer:
Every sock has a pair
Except for one sock that has no pair
If the socks are neatly arranged, you can find the single sock faster by checking patterns rather than checking each sock one by one.
This is exactly how this problem works.
Problem Statement
You are given a sorted array where:
Every element appears exactly twice
Except one element,for which appears only once
Your task is to find that single element.
Example
Array: [1, 1, 2, 3, 3, 4, 4]
Output: 2
Before vs After Understanding
Linear Scan (Before)
Check elements one by one
Compare neighbors
Works, but slow
Binary Search (After)
Use index patterns
Skip half of the array
Much faster
Key Observation (Very Important)
In a correctly paired array:
Pairs start at even index
First element of each pair is at even index
Second element is at odd index
Once the single element appears, this pattern breaks.
Why Binary Search Works Here
Because the array is:
Sorted
Mostly following a strict index pattern
Binary search helps us quickly find where the pattern breaks.
What Interviewers Are Actually Testing
Interviewers want to see:
Can you observe patterns in data?
Can you modify binary search logic?
Can you solve without extra space?
This problem tests logical thinking, not memorization.
Key Idea (Very Simple)
Check the middle index
Make sure mid is even
If pair is correct, move right
If pair is broken, move left
Step-by-Step Logic
Set
low = 0,high = n - 1Find
midMake
mideven (if odd, subtract 1)If
arr[mid] == arr[mid + 1], move rightElse, move left
Continue until
low == high
That index contains the single element.
Dry Run Example
Array: [1, 1, 2, 3, 3, 4, 4]
Join the conversation! Your thoughts help the community grow.