Common Elements in Two Sorted Arrays
Last Updated :
23 Jul, 2025
Given two sorted arrays a[] and b[]. The task is to return the common elements of both arrays. If an element appears more than once in both array, we need to pick minimum of its two frequencies
Examples:
Input: a[] = [1, 1, 2, 2, 2, 4], b[] = [2, 2, 4, 4]
Output: [2, 2, 4]
Explanation: 2 and 4 are only common elements and 2 appears two times in both arrays.
Input: a[] = [1, 2], b[] = [3, 4]
Output: []
Explanation: No common elements.
Input: a[] = [1, 1, 1], b[] = [1, 1, 1, 1]
Output: [1, 1, 1]
Approaches Same as Unsorted Arrays - Best Time O(n) and O(n) Space
We have discussed different approaches for common of two unsorted arrays. We can use the same approaches here. The best performance that we can achieve using the same approaches is O(n) Time and O(n) Auxiliary Space for hash set. Please note that in these approaches we do not use the fact that input arrays are sorted,
Using Merge of Merge Sort - O(n) Time and O(1) Space
The idea is based one merge function to merge two sorted arrays.
- We simultaneously traverse both a[] and b[] from the left side.
- If current elements are not same, we skip the smaller of the two. If current element of a[] is smaller, we move ahead in a[] and if current of b[] is smaller, we move ahead in b[].
- If same, we keep adding while elements are same.
C++
// C++ program to find common elements in two sorted
// array using merge of merge sort
#include <iostream>
#include <vector>
using namespace std;
vector<int> findCommon(vector<int>& a, vector<int>& b) {
vector<int> res;
int i = 0, j = 0;
int m = a.size(), n = b.size();
while (i < m && j < n) {
// Not Same : Skip the snaller
if (a[i] < b[j])
i++;
else if (a[i] > b[j])
j++;
// We reach here when both are same
while (i < m && j < n && a[i] == b[j]) {
res.push_back(a[i]);
i++;
j++;
}
}
return res;
}
int main() {
vector<int> a = {1, 1, 2, 2, 2, 4};
vector<int> b = {1, 2, 4};
vector<int> result = findCommon(a, b);
for (int x : result) {
cout << x << " ";
}
return 0;
}
C
// C program to find common elements in two sorted
// array using merge of merge sort
#include <stdio.h>
void findCommon(int a[], int sizeA, int b[], int sizeB, int res[], int *res_size) {
int i = 0, j = 0;
while (i < sizeA && j < sizeB) {
// Not Same: Skip the smaller
if (a[i] < b[j])
i++;
else if (a[i] > b[j])
j++;
// We reach here when both are same
while (i < sizeA && j < sizeB && a[i] == b[j]) {
res[(*res_size)++] = a[i];
i++;
j++;
}
}
}
int main() {
int a[] = {1, 1, 2, 2, 2, 4};
int b[] = {1, 2, 4};
int res[10];
int res_size = 0;
findCommon(a, 6, b, 3, res, &res_size);
for (int i = 0; i < res_size; i++) {
printf("%d ", res[i]);
}
return 0;
}
Java
// Java program to find common elements in two sorted
// array using merge of merge sort
import java.util.ArrayList;
import java.util.List;
class GfG {
static List<Integer> findCommon(int[] a, int[] b) {
List<Integer> res = new ArrayList<>();
int i = 0, j = 0;
int m = a.length, n = b.length;
while (i < m && j < n) {
// Not Same: Skip the smaller
if (a[i] < b[j])
i++;
else if (a[i] > b[j])
j++;
// We reach here when both are same
while (i < m && j < n && a[i] == b[j]) {
res.add(a[i]);
i++;
j++;
}
}
return res;
}
public static void main(String[] args) {
int[] a = {1, 1, 2, 2, 2, 4};
int[] b = {1, 2, 4};
List<Integer> result = findCommon(a, b);
for (int x : result) {
System.out.print(x + " ");
}
}
}
Python
# Python program to find common elements in two sorted
# array using merge of merge sort
def findCommon(a, b):
res = []
i, j = 0, 0
m, n = len(a), len(b)
while i < m and j < n:
# Not Same: Skip the smaller
if a[i] < b[j]:
i += 1
elif a[i] > b[j]:
j += 1
# We reach here when both are same
while i < m and j < n and a[i] == b[j]:
res.append(a[i])
i += 1
j += 1
return res
a = [1, 1, 2, 2, 2, 4]
b = [1, 2, 4]
result = findCommon(a, b)
print(" ".join(map(str, result)))
C#
// C# program to find common elements in two sorted
// array using merge of merge sort
using System;
using System.Collections.Generic;
class GfG {
static List<int> findCommon(int[] a, int[] b) {
List<int> res = new List<int>();
int i = 0, j = 0;
int m = a.Length, n = b.Length;
while (i < m && j < n) {
// Not Same: Skip the smaller
if (a[i] < b[j])
i++;
else if (a[i] > b[j])
j++;
// We reach here when both are same
while (i < m && j < n && a[i] == b[j]) {
res.Add(a[i]);
i++;
j++;
}
}
return res;
}
static void Main() {
int[] a = {1, 1, 2, 2, 2, 4};
int[] b = {1, 2, 4};
List<int> result = findCommon(a, b);
foreach (int x in result) {
Console.Write(x + " ");
}
}
}
JavaScript
// JavaScript program to find common elements in two sorted
// array using merge of merge sort
function findCommon(a, b) {
const res = [];
let i = 0, j = 0;
const m = a.length, n = b.length;
while (i < m && j < n) {
// Not Same: Skip the smaller
if (a[i] < b[j])
i++;
else if (a[i] > b[j])
j++;
// We reach here when both are same
while (i < m && j < n && a[i] === b[j]) {
res.push(a[i]);
i++;
j++;
}
}
return res;
}
const a = [1, 1, 2, 2, 2, 4];
const b = [1, 2, 4];
const result = findCommon(a, b);
console.log(result.join(" "));
Illustration of the Above Approach:
Input : a = {1, 2, 2, 2, 3}, b = {2, 2, 4}
Initialization:
i = 0
, j = 0
, res = []
i
points to elements in array a[]
and j
points to elements in array b[]
.
Iterations inside the while loop:
1st Iteration:
a[i] = 1
, b[j] = 2
a[i]
is smaller, so skip the smaller element in a[]
.i = 1
, j = 0
2nd Iteration:
a[i] = 2
, b[j] = 2
- Since
a[i]
is equal to b[j]
, both arrays have this common element. So we keep adding to result while matching. - res = [2, 2]
- i = 3, j = 2
3rd Iteration:
a[i] = 2
, b[j] = 4
- Since
a[i]
is smaller than b[j]
, skip the smaller element in a[]
by incrementing i
. - Update:
i = 4
, j = 2
4th Iteration:
a[i] = 3
, b[j] = 4
- Since
a[i]
is smaller than b[j]
, skip the smaller element in a[]
by incrementing i
. - Update:
i = 5
, j = 2
Similar Reads
Find common elements in three sorted arrays Given three sorted arrays in non-decreasing order, print all common elements in non-decreasing order across these arrays. If there are no such elements return an empty array. In this case, the output will be -1.Note: In case of duplicate common elements, print only once.Examples: Input: arr1[] = [1,
12 min read
Find Common Elements in Two Arrays in Python Given two arrays arr1[] and arr2[], the task is to find all the common elements among them. Examples: Input: arr1[] = {1, 2, 3, 4, 5}, arr2[] = {3, 4, 5, 6, 7}Output: 3 4 5Explanation: 3, 4 and 5 are common to both the arrays. Input: arr1: {2, 4, 0, 5, 8}, arr2: {0, 1, 2, 3, 4}Output: 0 2 4Explanati
8 min read
Find relative complement of two sorted arrays Given two sorted arrays arr1 and arr2 of size m and n respectively. We need to find relative complement of two array i.e, arr1 - arr2 which means that we need to find all those elements which are present in arr1 but not in arr2. Examples: Input : arr1[] = {3, 6, 10, 12, 15} arr2[] = {1, 3, 5, 10, 16
10 min read
Print uncommon elements from two sorted arrays Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements ar
6 min read
Merge two sorted arrays Given two sorted arrays, the task is to merge them in a sorted manner.Examples: Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8} Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8}Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8} Output: arr3[] = {4, 5, 7, 8, 8, 9} Table of Content[Naive Approach] Concatenat
10 min read
Intersection of Two Sorted Arrays Given two sorted arrays a[] and b[], the task is to return intersection. Intersection of two arrays is said to be elements that are common in both arrays. The intersection should not count duplicate elements and the result should contain items in sorted order.Examples:Input: a[] = {1, 1, 2, 2, 2, 4}
12 min read