VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-common-elements-three-sorted-arrays/

⇱ Common in 3 Sorted Arrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Common in 3 Sorted Arrays

Last Updated : 21 Apr, 2026

Given three sorted arrays in non-decreasing order, find all common elements in non-decreasing order across these arrays. If there are no such elements return an empty array.

Note: In case of duplicate common elements, include each element only once in the result.

Examples:

Input: a[] = [1, 5, 10, 20, 30], b[] = [5, 13, 15, 20], c[] = [5, 20] 
Output: 5 20
Explanation: 5 and 20 are common in all the arrays.

Input: a[] = [1, 2, 3, 4, 5], b[] = [6, 7], c[] = [8, 9, 10]
Output: []
Explanation: There are no common elements in a, b and c.

Input: a[] = [2, 5, 10, 30], b[] = [5, 20, 34], c[] = [5, 13, 19]
Output: 5
Explanation: 5 is common in all the arrays.

[Naive Approach] Using Hash Map or Dictionary

The idea is to use a Hash Map to track elements common across all three arrays while avoiding duplicates. Each element is marked step-by-step to identify those present in all arrays, and the final result is sorted to maintain non-decreasing order.

Dry run for a[] = [1, 5, 10, 20, 30], b[] = [5, 13, 15, 20], c[] = [5, 20]:

  1. Process array a, store unique elements as key and value as 1, count = {1:1, 5:1, 10:1, 20:1, 30:1}
  2. Process array b, update only common elements 5 and 20 from 1 to 2, count = {1:1, 5:2, 10:1, 20:2, 30:1}
  3. Process array c, update only common elements 5 and 20 from 2 to 3, count = {1:1, 5:3, 10:1, 20:3, 30:1}
  4. Collect elements with value = 3, common = [5, 20]
  5. Sort the result, common = [5, 20]

Final output, 5 20

Important Points:

  • We can also use a Self Balancing BST (like Tree Map) which maintains elements in sorted order automatically.
  • However, in languages like Python and JavaScript, there is no built-in Self Balancing BST, so we use a Hash Map or Dictionary for processing and then sort the result at the end.

Output
5 20 

Time Complexity: O((n1 + n2 + n3) + K log K), because we traverse all arrays once and sort K common elements.
Auxiliary Space: O(n1), because the hash map stores elements from the first array.

[Expected Approach] Using three pointers - O(n1 + n2 + n3) Time and O(1) Space

We can efficiently find common elements in three sorted arrays using the three-pointer technique, eliminating the need for merging or intersecting the arrays.

Since the arrays are sorted in non-decreasing order, the smallest element among the three pointers at any moment will always be the smallest in the merged arrays. Leveraging this property, we can optimize our approach as follows:

  • Initialize three pointers, one for each array.
  • Compare the current elements at each pointer.
  • If they are equal, it's a common element, store it and move all three pointers forward.
  • Otherwise, move only the pointer pointing to the smallest element.
  • Repeat the process until at least one array is fully traversed.

Working of Three pointers approach:



Output
5 20 
Comment
Article Tags: