Find Common Elements in Two Sorted Arrays



To find common elements in two sorted arrays using JavaScript, we will be discussing various approaches. Common element refers to element which is present in both the arrays. First, we will try brute force approach and then we will optimize our code to improve the time complexity.

In this article we are having two sorted arrays, our task is to find common elements in two sorted arrays. Users must already know about JavaScript arrays, its function and operations on arrays, loops, searching technique and conditional statement.

Common elements

Example

Input:
arr1 = [1, 2, 3, 4, 5]
arr2 = [2, 3, 5, 7, 8]

Output:
Common elements: [2, 3, 5]

Approaches to Find Common Elements

Here is a list of approaches to find common elements in two sorted arrays which we will be discussing in this article with stepwise explanation and complete example codes.

Using Brute Force

To find common elements in two sorted arrays, first approach we are using is brute force method. It is simple method where we try all possible combination to find common elements.

  • First we have declared two arrays as array1 and array2.
  • Then we have defined a function common where we have passed two arrays, arr1 and arr2 as its arguments.
  • We have used an empty array res to store the common elements of both arrays and used two for loops where outer loop iterates over array arr1 and inner loop iterates over array arr2.
  • For each element of array 1(outer loop), inner loop iterates over all elements of arr2 and check if they are equal. If element of arr1 is equal to element of arr2, then that element is pushed in array res using push() operation and we break the inner loop.
  • After comparison of each element of arr1 with every element of arr2, the array res is returned. Then we have called the function common by passing array1 and array2 as its arguments and is stored in commonElement.
  • Then the output commonElement is displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to find common elements in two sorted arrays using brute force method.

let array1 = [1, 2, 4, 5, 3];
let array2 = [2, 3, 5, 7, 8];

function common (arr1, arr2){
    let res = [];
    for (let i = 0; i 



In this approach to find common elements in two sorted arrays, we have used binary search. Here, binary search is used to compare the elements of array2 with array1.

  • First we have declared two arrays as array1 and array2.
  • Then we have defined two functions: binSearch() and common() where, binSearch() function searches for the target in the array arr and common() function uses binSearch() function to compare arr2 with elements of arr1.
  • In binSearch() function, first we search for the middle element. Middle element is calculated using adding left and right element index and dividing it by 2. The while loop is used to define the search range that is search untill l<=r where l is left boundary and r is the right boundaary.
  • Then if/else statement is used to compare the target with array elements. If target is the middle element then it returns true, othherwise it increases the left index by 1 if middle element is less than the target and right index is reduced by 1 if middle element is greater than target.
  • Then we have used common() function in which we have passed arr1 and arr2 as its arguments. The common() function uses binSearch() function to find num in arr2, where num represents each element of arr1.
  • If element of arr1 is equal to element of arr2, then that element is pushed in array res using push() operation. After comparison of each element of arr1 with every element of arr2, the array res is returned.
  • Then we have called the function common() by passing array1 and array2 as its arguments and is stored in commonElement and output commonElement is displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to find common elements in two sorted arrays using binary search.

let array1 = [1, 2, 4, 5, 6];
let array2 = [2, 3, 5, 7, 8];


function binSearch(arr, target) {
    let l = 0, r = arr.length - 1;
    while (l 


Using Two Pointer

In this approach we have used two pointer method to find common elements in two sorted arrays. We are using two pointers to traverse the arrays in same direction.

  • First we have declared two arrays as array1 and array2 and defined a function common() that takes arr1 and arr2 as its arguments.
  • Then we have defined two pointers i.e i for arr1 and j for arr2. We have used while loop to iterate over elements till they reach their last element using length property.
  • We have used if/else condition to compare elements of both arrays. If the elements are same in both arrays then the common element is pushed into array res and both pointers are increased by 1.
  • If element of arr1 is smaller than element of arr2 then only i is increased and vice-versa.
  • After comparison of each element of arr1 with every element of arr2, the array res is returned. Then we have called the function common by passing array1 and array2 as its arguments and is stored in commonElement.
  • Then the output commonElement is displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to find common elements in two sorted arrays using two pointer method.

let array1 = [1, 2, 4, 5, 6];
let array2 = [2, 3, 5, 7, 8];

function common(arr1, arr2) {
    let i = 0, j = 0;
    let res = [];
    while (i 

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Brute Force O(n.m) O(min(n,m))
Binary Search O(n?logm) O(n)
Two Pointer O(n+m) O(n)

Conclusion

In this article, to find common elements in two sorted arrays we have discussed three different approaches which are: using brute force method, using binary search method and using two pointers method. Considering sorted arrays, Two pointer method is the most efficient approach out of three.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2024-12-02T18:56:44+05:30

416 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements