In less-than-linear time, find the duplicate in a sorted array
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Finding duplicates in an array is a common problem in computer science with numerous applications in data processing, error-checking systems, and more. In this article, we'll explore finding a duplicate in a sorted array in less-than-linear time. By leveraging the already sorted structure of the array, we can perform this operation efficiently. We'll examine the technical details, provide examples, and summarize key points.
Problem Statement
Given a sorted array, our objective is to identify any duplicate element in the array with time complexity better than , where is the size of the array.
Prerequisites
- Sorted Array: The array is sorted in non-decreasing order.
- Duplicate Exists: At least one duplicate exists within the array.
Approaches to Finding a Duplicate
Using Binary Search
The key idea when dealing with sorted arrays is to leverage binary search, which operates in time complexity. Below is a binary search-based approach to find a duplicate:
- Concept: In a sorted array, duplicates will always be adjacent. Therefore, if we split the array at any midpoint, at least one half must contain an extra copy of a number.
- Algorithm:
- Initialize two pointers:
lowto the start (0) andhighto the end (n-1) of the array. - While
low < high:- Find the mid-point:
mid = low + (high - low) / 2. - Count numbers in the left half up to index
mid. - If the count is greater than the number of unique numbers expected in the left half:
- The duplicate must be in the left half. Set
high = mid.
- Otherwise:
- The duplicate is in the right half. Set
low = mid + 1.
- The duplicate number is found when
low == high.
Example Code (C++)
Time Complexity Analysis
Using binary search in this way has a time complexity of , where the factor arises from counting elements up to mid in each iteration. However, this approach is efficient given the logarithmic reduction in search space.
Key Insights
- Data Structure: Knowing that the array is sorted helps in significantly reducing the problem space. Binary search is naturally suited for sorted datasets.
- Adjacent Duplicates: In a sorted array, duplicates lie next to each other, simplifying the detection.
Summary Table
| Key Aspect | Details |
| Data Requirement | Sorted, contains duplicates |
| Optimal Algorithm | Binary Search |
| Time Complexity | |
| Space Complexity | (original array not altered) |
| Example (Duplicate Element) | [1, 2, 2, 3, 4] - Duplicate is 2 |
Conclusion
Finding duplicates in a sorted array can be done efficiently using binary search, taking advantage of the array’s sorted nature to achieve a time complexity of . This approach is optimal in scenarios where the array is large and performance is crucial. Implementing this method provides a robust solution to the problem of detecting duplicates in less-than-linear time.
In future studies, exploring alternative data structures or integration with parallel algorithms could further enhance performance, especially on large datasets.
Related reading
- In Order Successor in Binary Search Tree
- In paxos, what happens if a proposer is down after its proposal is rejected?
- In Paxos, why can''t we use random backoff to avoid collision?
- In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique while preserving order?
- In memory queue server
- In Pika or RabbitMQ, How do I check if any consumers are currently consuming?
- In RAFT is it possible to have a majority consensus on a log entry but the entry is not committed?
- In simple terms, how is compression commonly implemented?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.