
- 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
Finding intersection of arrays that contain repetitive entries in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, lets call them arr1 and arr2.
The function should build a third array based on the two input arrays that contains all the elements that are common to both arr1 and arr2.
Note that if there are more than one same element that are present in both the arrays then we have to consider all such instances of that element.
For example −
If the input arrays are −
const arr1 = [1, 2, 2, 4, 4, 5, 6]; const arr2 = [3, 2, 4, 2, 4, 9];
Then the output array should be −
const output = [2, 2, 4, 4];
Example
Following is the code −
const arr1 = [1, 2, 2, 4, 4, 5, 6]; const arr2 = [3, 2, 4, 2, 4, 9]; const findIntersection = (arr1 = [], arr2 = []) => { const map = new Map(); for (const el of arr2) { const count = map.get(el) || 0; map.set(el, count + 1); }; return arr1.filter(el => { let count = map.get(el); if (count) { map.set(el, --count); return true; } return false; }); }; console.log(findIntersection(arr1, arr2));
Output
Following is the console output −
[2, 2, 4, 4]
- Related Articles
- Finding intersection of multiple arrays - JavaScript
- Finding intersection of arrays of intervals in JavaScript
- Finding the intersection of arrays of strings - JavaScript
- Unique intersection of arrays in JavaScript
- Intersection of two arrays JavaScript
- Intersection of three sorted arrays in JavaScript
- Finding the inclination of arrays in JavaScript
- Finding array intersection and including repeating elements in JavaScript
- Finding reversed index of elements in arrays - JavaScript
- Finding the continuity of two arrays in JavaScript
- JavaScript Program for Finding Intersection of Two Sorted Linked Lists
- JavaScript Program for Finding Intersection Point of Two Linked Lists
- Intersection of two arrays in Java
- Intersection of two arrays in C#
- Intersection of Two Arrays in C++

Advertisements