How to intersect two sorted integer arrays without duplicates?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- How to iterate over n dimensions?
- How to iterate through SparseArray?
- How to keep track of depth in breadth first search?
- How to know if a binary number divides by 3?
- How to invert a permutation array in numpy
- How to iterate over a list in chunks
- How to know when a recursive, asynchronous task finishes
- How to know when Big O is Logarithmic?

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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.