The intersection of two sorted arrays
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 computer science, the intersection of two sorted arrays is a common problem that often arises in various applications like database management, search engines, and data analysis. The intersection refers to the set of elements that appear in both input arrays. This article delves into different methods to achieve this intersection efficiently, considering the inherent sorted nature of the input arrays.
Problem Statement
Given two sorted arrays, find the intersection of these arrays with each element appearing as many times as it shows in both arrays. The result should also be sorted.
Algorithmic Approaches
Two-Pointer Technique
The most efficient method for finding the intersection of two sorted arrays involves using the Two-Pointer Technique. This method leverages the fact that the arrays are already sorted, allowing us to traverse both arrays simultaneously.
Algorithm
- Initialize two pointers,
ifor the first array andjfor the second array, both starting at the beginning (zero index). - While both pointers are within the bounds of their respective arrays:
- If
arr1[i] < arr2[j], incrementi. - If
arr1[i] > arr2[j], incrementj. - If
arr1[i] == arr2[j], record the element (as it is part of the intersection) and increment bothiandj.
- Continue until you reach the end of either array.
Example
Consider two sorted arrays:
arr1 = [1, 2, 4, 5, 6]arr2 = [2, 3, 5, 7]
Using the two-pointer technique:
- Start with
i = 0andj = 0. - Compare
arr1[0]andarr2[0], i.e.,1and2. - Since
1 < 2, incrementito1. - Now,
arr1[1]is2andarr2[0]is still2. - Elements are equal, record
2, increment bothiandj. - The process continues until the end of both arrays, resulting in the intersection
[2, 5].
Time and Space Complexity
- Time Complexity: , where
nandmare the lengths of the two arrays. Each element is visited at most once. - Space Complexity: , excluding the space required for the output.
Binary Search Method
For cases where the sizes of the arrays are significantly different, a binary search approach might be more efficient. This approach iteratively performs a binary search for elements of the smaller array in the larger array, ensuring that the complexity remains manageable.
Algorithm
- Iterate through each element of the smaller array.
- For each element, apply binary search on the larger array.
- If a match is found, include the element in the result set.
Time Complexity Analysis
- Time Complexity: or , depending on which array is smaller.
- Space Complexity: , similar to the two-pointer technique.
Implementation Notes
When implementing these algorithms, edge cases such as empty arrays and arrays with no intersection must be considered. Both approaches can be adapted to handle these without additional overhead.
Comparison Table
| Method | Time Complexity | Space Complexity | Suitable for |
| Two-Pointer | Arrays of similar size (both sorted) | ||
| Binary Search | Arrays of different sizes (both sorted) |
Additional Considerations
Unsorted Arrays
For unsorted arrays, the above methods are not directly applicable without first sorting the arrays. This can be done using a sorting algorithm like Merge Sort, which will add an complexity to the entire process. Alternatively, hash-based methods can be used to achieve a balance between time and space efficiency.
Duplicate Handling
If the arrays contain duplicates, both of the aforementioned methods inherently handle duplicates by checking equality before recording an element as part of the intersection.
Practical Applications
- Database Systems: Determining common records between tables.
- Search Engines: Finding common terms between different documents.
- Data Analysis Tools: Intersecting categories or tags between datasets to find commonalities.
Understanding the intersection of two sorted arrays is crucial in many fields where data efficiency and manipulation are critical. By leveraging the sorted property of arrays, the two-pointer and binary search methods offer efficient and scalable solutions to this problem.
Related reading
- The loss function and evaluation metric of XGBoost
- The max product of consecutive elements in an array
- The Maximum Volume of Trapped Rain Water in 3D
- The Most Efficient Way To Find Top K Frequent Words In A Big Word Sequence
- The opposite of Intersect
- The order of elements in Dictionary
- The most efficient way to implement an integer based power function powint, int
- the number of trailing zeros in a factorial of a given number - Ruby

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.