Find Common Elements In Three Sorted Arrays using JavaScript
Last Updated :
04 Apr, 2024
JavaScript can be used to find the common elements in given three sorted arrays. We are given three different sorted arrays, we have to return common elements of three arrays.
Example:
Input
array1 = [1 , 2 , 3 , 4 ,5 ]
array2 = [3 , 4, 5 , 6 ,7 ]
array3 = [ 3 , 4 , 7 , 8 , 9]
Output
[3 , 4]
Below are different approaches to finding common elements present in three sorted arrays:
Brute force Approach
We will initialize three pointers to traverse each element of the array simultaneously. Now we iterate through the arrays using the pointers within its size limits and compare element present at the current positions of pointers and push common elements into the common array. Now we increase pointers comparing elements present in three arrays and then return the common array.
Example: To demonstrate finding common elements present in three sorted arrays using three pointers approach
JavaScript
function CommonElements(arr1, arr2, arr3) {
const common = [];
let i = 0, j = 0, k = 0;
while (i < arr1.length
&& j < arr2.length
&& k < arr3.length
) {
if (
arr1[i] === arr2[j]
&&
arr2[j] === arr3[k]
) {
common.push(arr1[i]);
i++;
j++;
k++;
}
else if (arr1[i] < arr2[j]) {
i++;
}
else if (arr2[j] < arr3[k]) {
j++;
}
else {
k++;
}
}
return common;
}
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 5, 6, 7, 8];
const arr3 = [3, 4, 5, 8, 1];
console
.log("common elements in three array are :",
CommonElements(arr1, arr2, arr3));
Outputcommon elements in three array are : [ 4, 5 ]
Time complexity: O(n) , n is total numbers of elements across all three arrays
Space complexity : O(m) , m is common elements present in arrays
Using set
We will create sets from all three input arrays to remove duplicate elements. Now we Iterate through the elements of the first set and check for each element that it exist in both second and third sets. if element exist in both sets then we add it to common array. Return the common array.
Example : To demonstrate finding common elements present in three sorted arrays using set
JavaScript
function CommonElements(arr1, arr2, arr3) {
const set1 = new Set(arr1);
const set2 = new Set(arr2);
const set3 = new Set(arr3);
const common = [];
for (const elem of set1) {
if (
set2.has(elem)
&&
set3.has(elem)) {
common.push(elem);
}
}
return common;
}
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 5, 6, 7, 8];
const arr3 = [2, 4, 5, 1];
console
.log("common elements in three array using set : ",
CommonElements(arr1, arr2, arr3));
Outputcommon elements in three array using set : [ 4, 5 ]
Time complexity : O(n+m)
Space complexity: O(n+m)
Using Hashmap
We will create a hash map to store the count of occurrences of elements from arr1. Now we Iterate through arr2, if an element is found in map, push it into the common array and decrement its count in map. Finally we iterate through arr3 and similarly push elements into finalCommon array if found in map and decrement their count. Return the finalCommon array
Example : To demonstrate finding common elements present in three sorted arrays using hashmap.
JavaScript
function findCommonElements(arr1, arr2, arr3) {
const map1 = {};
const map2 = {};
const map3 = {};
const common = [];
for (const num of arr1) {
map1[num] = (map1[num]
||
0
) + 1;
}
for (const num of arr2) {
map2[num] = (
map2[num]
||
0
) + 1;
}
for (const num of arr3) {
map3[num] = (
map3[num]
||
0
) + 1;
}
for (const num of arr1) {
if (
map1[num]
&&
map2[num]
&&
map3[num]
) {
common
.push(num);
map1[num]--;
map2[num]--;
map3[num]--;
}
}
return common;
}
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 5, 6, 7, 8];
const arr3 = [1, 4, 5, 2, 6];
console
.log("common elements in three array is : ",
findCommonElements(arr1, arr2, arr3));
Outputcommon elements in three array is : [ 4, 5 ]
Time complexity : O(n + m + p)
Space complexity : O(min(n,m,p))
Similar Reads
Find the Common Elements of More than Two JavaScript Arrays? Given an HTML document having multiple arrays with some elements and the task is to get the common elements from arrays with the help of JavaScript. Below are the approaches to find the common elements of more than two JavaScript Arrays:Table of ContentUsing filter methodUsing reduce method Approach
3 min read
Find Kth Element of Two Sorted Arrays in JavaScript Given two sorted arrays, our task is to find the Kth element in the combined array made by merging the two input arrays in JavaScript.Example:Input: Arr1: [1, 2, 5] , Arr2:[2, 4, 6, 8], K = 4Output: Kth element is 4.Explanation:The final Array would be: [1, 2, 2, 4, 5, 6, 8]The 4th element of this a
3 min read
Find the min/max element of an Array using JavaScript To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator.JavaScript offers several methods to achieve this, each with its advantages.Using Math.min() and Math.max() Methods The Math object's Math.min() and Math.max() methods are static methods t
2 min read
Find Second Smallest Element in an Array in JavaScript JavaScript allows us to compute the Second Smallest Element within a given array. The task involves iterating through the array, identifying the smallest and second smallest element in the array. There are several approaches to finding the second smallest element in an array using Javascript which a
2 min read
JavaScript Program to Find Largest Element in an Array In this article, we are going to learn about the largest element in an array in JavaScript. The largest element in an array refers to the value that holds the greatest numerical or lexicographic (string) order among all elements present in the array. Example: Input : [10, 15, 38, 20, 13];Output: 38H
3 min read
JavaScript â Min Element in an Array These are the following ways to find the minimum element in JS array:Note: You must add a conditional check for checking whether the given array is empty or not, if the array is not empty then you can use the given below approaches to find out the minimum element among them.1. Using Math.min() Metho
3 min read
How to Sort an Array Based on the Length of Each Element in JavaScript? Imagine you have a list of words or groups of items, and you want to arrange them in order from shortest to longest. This is a pretty common task in JavaScript, especially when working with text or collections of things. By sorting your list in this way, you can make sense of your data and make it e
3 min read
Find Mode of an Array using JavaScript To find the mode in an array with JavaScript, where the mode is the number occurring most frequently. Various approaches can be employed to find the mode, and an array may have multiple modes if multiple numbers occur with the same highest frequency. Example:Input:3, 6, 4, 6, 3, 6, 6, 7 , 6, 3 , 3Ou
4 min read
How to find every element that exists in any of two given arrays once using JavaScript ? In this article, we will learn how to find every element that exists in any of the given two arrays. To find every element that exists in any of two given arrays, you can merge the arrays and remove any duplicate elements. Table of Content Using SetUsing loopUsing filter() and concat()Using reduce a
3 min read
How to get n largest elements from array in JavaScript ? Here in this article, we will see how we can find the n maximum element from the array using Javascript. Example: Input: arr = [1, 2, 3, 4, 5, 6], n = 3;Output: 4, 5, 6Explanation: Here we will see the 3 largest elements in the given array are 4, 5, 6. Input: arr = [5, 76, 32, 98, 52, 57] n = 2;Outp
3 min read