
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Intersection of three sorted arrays in JavaScript
We are required to write a JavaScript function that takes in three arrays of integers all sorted in an increasing order. The function should then construct and return an array that contains only those elements that present in all three arrays.
For example −
If the input arrays are −
const arr1 = [4, 7, 8, 11, 13, 15, 17]; const arr2 = [1, 3, 4, 13, 18]; const arr3 = [2, 4, 7, 8, 9, 10, 13];
Then the output should be −
const output = [4, 13];
Example
The code for this will be −
const arr1 = [4, 7, 8, 11, 13, 15, 17]; const arr2 = [1, 3, 4, 13, 18]; const arr3 = [2, 4, 7, 8, 9, 10, 13]; const intersectThree = (arr1 = [], arr2 = [], arr3 = []) => { let curr1 = 0; let curr2 = 0; let curr3 = 0; const res = []; while((curr1 < arr1.length) && (curr2 < arr2.length) && (curr3 < arr3.length)){ if((arr1[curr1] === arr2[curr2]) && (arr2[curr2] === arr3[curr3])){ res.push(arr1[curr1]); curr1++; curr2++; curr3++; } const max = Math.max(arr1[curr1], arr2[curr2], arr3[curr3]); if(arr1[curr1] < max){ curr1++; }; if(arr2[curr2] < max){ curr2++; }; if(arr3[curr3] < max){ curr3++; }; }; return res; }; console.log(intersectThree(arr1, arr2, arr3));
Output
And the output in the console will be −
[4, 13]
- Related Articles
- Intersection of Three Sorted Arrays in C++
- Find common elements in three sorted arrays by dictionary intersection in Python
- Unique intersection of arrays in JavaScript
- Intersection of two arrays JavaScript
- Finding intersection of multiple arrays - JavaScript
- Finding intersection of arrays of intervals in JavaScript
- Finding the intersection of arrays of strings - JavaScript
- Find three closest elements from given three sorted arrays in C++
- Find common elements in three sorted arrays in C++
- Finding intersection of arrays that contain repetitive entries in JavaScript
- Merging sorted arrays together JavaScript
- Java program to find common elements in three sorted arrays
- Python program to find common elements in three sorted arrays?
- C# program to find common elements in three sorted arrays
- Intersection of two arrays in Java

Advertisements