
- 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 two arrays JavaScript
We have two arrays of Numbers, and we are required to write a function, say intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.
For example − If,
Input: arr1 = [1,2,3,1], arr2 = [1,3,1] Output: [1,3,1]
Approach
Had the arrays been sorted, we could have used the two pointer approach with initially both pointing to 0 the start of the respective array and we could have proceeded with increasing the corresponding pointer and that would have been O(m+n) complex w.r.t. time where m and n are the sizes of array.
But since we have unsorted arrays there is no logic in sorting the arrays and then using this approach, we will check every value of first against the second and construct an intersection array. This would cost us O(n^2) time.
And the code for doing this will be −
Example
const arr1 = [1, 2, 43, 5, 3, 7, 7,8, 4, 2]; const arr2 = [1, 1, 6, 6, 2, 78, 7, 2, 3, 7, 23, 5, 3]; const intersection = (arr1, arr2) => { const res = []; const { length: len1 } = arr1; const { length: len2 } = arr2; const smaller = (len1 < len2 ? arr1 : arr2).slice(); const bigger = (len1 >= len2 ? arr1 : arr2).slice(); for(let i = 0; i < smaller.length; i++){ if(bigger.indexOf(smaller[i]) !== -1){ res.push(smaller[i]); bigger.splice(bigger.indexOf(smaller[i]), 1, undefined); } }; return res; }; console.log(intersection(arr1 ,arr2));
Output
The output in the console will be −
[1, 2, 5, 3, 7, 7, 2]
- Related Articles
- Intersection of two arrays in Java
- Intersection of two arrays in C#
- Intersection of Two Arrays in C++
- Finding intersection of multiple arrays - JavaScript
- Unique intersection of arrays in JavaScript
- Intersection of Two Arrays II in Python
- Intersection of three sorted arrays in JavaScript
- How to Create an Array using Intersection of two Arrays in JavaScript?
- Finding the intersection of arrays of strings - JavaScript
- Finding intersection of arrays of intervals in JavaScript
- How to find intersection between two Numpy arrays?
- How to get the intersection of two arrays in MongoDB?
- How to find the intersection of two arrays in java?
- Find Union and Intersection of two unsorted arrays in C++
- Finding intersection of arrays that contain repetitive entries in JavaScript
