Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript function that takes a multidimensional and a single array, and finds matches of the single array in the multi-d array
We need to write a JavaScript function that takes a multidimensional array (array of arrays) as the first argument and a single array as the second argument. The function should find common elements between each subarray and the single array, returning a new array containing only the matching elements from each subarray.
Problem Understanding
For each subarray in the multidimensional array, we want to find elements that also exist in the single array. The result preserves the structure but only includes common elements.
For example, if we have:
const arr1 = [
[1,2,5,6],
[5,13,7,8],
[9,11,13,15],
[13,14,15,16],
[1,9,11,12]
];
const arr2 = [9,11,13,15,1,2,5,6];
The expected output should be:
[
[1, 2, 5, 6], // All elements from [1,2,5,6] exist in arr2
[5, 13], // Only 5 and 13 from [5,13,7,8] exist in arr2
[9, 11, 13, 15], // All elements exist in arr2
[13, 15], // Only 13 and 15 from [13,14,15,16] exist in arr2
[1, 9, 11] // Only 1, 9, and 11 from [1,9,11,12] exist in arr2
]
Using Array Methods (Recommended)
const arr1 = [
[1,2,5,6],
[5,13,7,8],
[9,11,13,15],
[13,14,15,16],
[1,9,11,12]
];
const arr2 = [9,11,13,15,1,2,5,6];
const findIntersection = (multiArray, singleArray) => {
return multiArray.map(subArray => {
return subArray.filter(element => singleArray.includes(element));
});
};
console.log(findIntersection(arr1, arr2));
[ [ 1, 2, 5, 6 ], [ 5, 13 ], [ 9, 11, 13, 15 ], [ 13, 15 ], [ 1, 9, 11 ] ]
Using Set for Better Performance
For larger arrays, using a Set provides O(1) lookup time instead of O(n) with includes():
const findIntersectionOptimized = (multiArray, singleArray) => {
const targetSet = new Set(singleArray);
return multiArray.map(subArray => {
return subArray.filter(element => targetSet.has(element));
});
};
const arr1 = [
[1,2,5,6],
[5,13,7,8],
[9,11,13,15]
];
const arr2 = [9,11,13,15,1,2,5,6];
console.log(findIntersectionOptimized(arr1, arr2));
[ [ 1, 2, 5, 6 ], [ 5, 13 ], [ 9, 11, 13, 15 ] ]
Preserving Original Order
If you need to maintain the order of elements as they appear in the single array rather than the subarray order:
const findIntersectionOrdered = (multiArray, singleArray) => {
return multiArray.map(subArray => {
const subSet = new Set(subArray);
return singleArray.filter(element => subSet.has(element));
});
};
const arr1 = [[6,5,2,1], [8,13,5,7]];
const arr2 = [1,2,5,6,13];
console.log("Original order (subarray):", findIntersection(arr1, arr2));
console.log("Target array order:", findIntersectionOrdered(arr1, arr2));
Original order (subarray): [ [ 6, 5, 2, 1 ], [ 13, 5 ] ] Target array order: [ [ 1, 2, 5, 6 ], [ 5, 13 ] ]
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| Array.includes() | O(n×m) | Small arrays, simple implementation |
| Set.has() | O(n+m) | Large arrays, better performance |
| Ordered approach | O(n+m) | When order matters |
Conclusion
Use the array filter and includes approach for simplicity, or Set-based solution for better performance with large datasets. The key is mapping over each subarray and filtering elements that exist in the target array.
