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
Finding Common Item Between Arbitrary Number of Arrays in JavaScript
Finding common elements among multiple arrays is a frequent requirement in JavaScript programming. This problem involves identifying elements that appear in every array within a collection of arrays.
What are Arbitrary Number of Arrays
An arbitrary number of arrays refers to any collection of multiple arrays (more than two) where we need to find intersection elements. These arrays can contain random elements and are typically organized as an array of arrays or nested data structure.
// Example: Array of arrays const arrayCollection = [ [1, 2, 3, 4], [2, 3, 5, 6], [3, 7, 8, 9] ]; // Common element: 3 (appears in all arrays)
Using Loop-Based Approach
The basic approach uses nested loops to compare the first array's elements against all other arrays:
function findCommonElements(arr) {
let commonArray = [];
// Iterate through first array elements
for (let i = 0; i < arr[0].length; i++) {
let isCommon = true;
// Check if current element exists in all other arrays
for (let j = 1; j < arr.length; j++) {
if (arr[j].indexOf(arr[0][i]) === -1) {
isCommon = false;
break;
}
}
if (isCommon) {
commonArray.push(arr[0][i]);
}
}
return commonArray;
}
const arrayOfArrays = [
[1, 4, 6, 78, 8, 9, 124, 44],
[44, 6, 9],
[124, 44, 16, 9]
];
console.log(findCommonElements(arrayOfArrays));
[ 1, 4, 6, 124, 44, 9 ]
Using Map and Set (Optimized)
A more efficient approach uses Map to count occurrences and Set to handle duplicates:
function findCommonElementsOptimized(mainArray) {
const elementCount = new Map();
const commonElements = [];
// Count occurrences of each element across all arrays
for (let subArray of mainArray) {
const uniqueElements = new Set(subArray);
uniqueElements.forEach(element => {
elementCount.set(element, (elementCount.get(element) || 0) + 1);
});
}
// Find elements that appear in all arrays
elementCount.forEach((count, element) => {
if (count === mainArray.length) {
commonElements.push(element);
}
});
return commonElements;
}
const arrays = [
[15, 23, 36, 49, 104, 211],
[9, 12, 23],
[11, 17, 18, 23, 38],
[13, 21, 23, 27, 40, 85]
];
console.log(findCommonElementsOptimized(arrays));
[ 23 ]
Using Array.filter() and every()
A functional programming approach using built-in array methods:
function findCommonElementsFunctional(arrays) {
if (arrays.length === 0) return [];
return arrays[0].filter(element =>
arrays.every(array => array.includes(element))
);
}
const testArrays = [
[1, 2, 3, 4, 5],
[2, 3, 4, 6, 7],
[3, 4, 8, 9, 10]
];
console.log(findCommonElementsFunctional(testArrays));
[ 3, 4 ]
Comparison of Approaches
| Method | Time Complexity | Space Complexity | Pros |
|---|---|---|---|
| Nested Loops | O(n × m × k) | O(1) | Simple, minimal memory |
| Map and Set | O(n × m) | O(k) | Better performance, handles duplicates |
| Functional Approach | O(n × m × k) | O(1) | Clean, readable code |
Where n = number of arrays, m = average array length, k = unique elements
Conclusion
The Map and Set approach offers the best balance of performance and readability for finding common elements across multiple arrays. Choose the functional approach for cleaner code when performance isn't critical, or the nested loop method for minimal memory usage.
