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 the intersection of arrays of strings - JavaScript
We need to find the intersection of two arrays of strings and return an array containing the common elements. Each element in the result should appear as many times as it shows in both arrays.
For example ?
If input is ?
arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate'];
Then the output should be ?
['world', 'you']
Approach
For unsorted arrays, we need to check every value of the first array against the second array. This approach has O(n²) time complexity. We iterate through the smaller array and check if each element exists in the bigger array using indexOf().
Using Basic Array Methods
const arr1 = ['hello', 'world', 'how', 'are', 'you'];
const arr2 = ['hey', 'world', 'can', 'you', 'rotate'];
const intersectElements = (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);
}
}
return res;
};
console.log(intersectElements(arr1, arr2));
[ 'world', 'you' ]
Using Set for Better Performance
For better performance with larger arrays, we can use a Set to avoid O(n²) complexity:
const arr1 = ['hello', 'world', 'how', 'are', 'you', 'world'];
const arr2 = ['hey', 'world', 'can', 'you', 'rotate', 'world'];
const intersectElementsSet = (arr1, arr2) => {
const set2 = new Set(arr2);
const result = [];
const used = new Map();
for(const item of arr1) {
if(set2.has(item)) {
const count2 = arr2.filter(x => x === item).length;
const usedCount = used.get(item) || 0;
if(usedCount < count2) {
result.push(item);
used.set(item, usedCount + 1);
}
}
}
return result;
};
console.log(intersectElementsSet(arr1, arr2));
[ 'world', 'you', 'world' ]
Using Filter Method
A more concise approach using the filter() method:
const arr1 = ['hello', 'world', 'how', 'are', 'you'];
const arr2 = ['hey', 'world', 'can', 'you', 'rotate'];
const intersectFilter = (arr1, arr2) => {
return arr1.filter(item => arr2.includes(item));
};
console.log(intersectFilter(arr1, arr2));
[ 'world', 'you' ]
Comparison of Methods
| Method | Time Complexity | Handles Duplicates | Readability |
|---|---|---|---|
| Basic Loop with indexOf | O(n²) | Yes | Medium |
| Set-based Approach | O(n + m) | Yes (with counting) | Complex |
| Filter with includes | O(n²) | No | High |
Conclusion
The Set-based approach offers the best performance for large arrays, while the filter method provides the most readable code. Choose based on your specific requirements for performance versus code simplicity.
