How to intersect two sorted integer arrays without duplicates?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In this article, we will explore an efficient algorithm to intersect two sorted integer arrays without duplicates. A common problem in programming, it's crucial in scenarios where you need to determine the common elements in distinct datasets while maintaining computational efficiency. Given that the arrays are already sorted, our approach will take advantage of this property to achieve optimal performance.
Problem Definition
Given two sorted arrays A and B, our goal is to find their intersection, where each element in the result should appear as many times as it shows in both arrays. Since arrays are sorted and without duplicates, this problem can be solved efficiently.
Algorithm Explanation
We will utilize a two-pointer technique, essentially having one pointer for each array, to effectively traverse through the arrays and find common elements. This approach capitalizes on the sorted nature of the arrays and achieves optimal time complexity.
Steps:
- Initialize Pointers: Start by initializing two pointers,
iandj, to traverse arraysAandB, respectively. - Traverse Arrays: Use a loop to iterate through both arrays simultaneously:
- If
A[i]is less thanB[j], incrementi. - If
A[i]is greater thanB[j], incrementj. - If
A[i]equalsB[j], it means we have found a common element. AddA[i](orB[j]) to the result and increment bothiandj.
- End of Array Check: The loop continues until we reach the end of one of the arrays.
Code Example
Complexity Analysis
- Time Complexity: The algorithm operates in time, where
nis the length of arrayAandmis the length of arrayB. This is due to each element in both arrays being traversed at most once. - Space Complexity: The solution takes space for the output array holding the common elements.
Considerations and Edge Cases
- Empty Arrays: If either input array is empty, the result will also be an empty array as there are no common elements.
- Different Array Lengths: The algorithm naturally handles arrays of different lengths, iterating only until the end of the shorter array.
- No Common Elements: If there are no intersecting elements, the result will be an empty array.
Summary Table
| Scenario / Consideration | Action Taken / Result |
| Both arrays are empty | Returns an empty list |
| One array is empty | Returns an empty list |
| No common elements | Returns an empty list |
| Arrays with common items | Returns a list of common items |
| Complexity | Time: ; Space: |
| Usage of two pointers | Efficient traversal leveraging sorted properties of the arrays |
Using the two-pointer technique, we achieve a linear traversal of both arrays, making our approach efficient and well-suited for scenarios requiring the intersection of sorted integer arrays without duplicates. This method is not only simple to implement but also provides optimal performance across a range of input sizes.

