A common task in data manipulation is to find out if two arrays have at least one element in common. This is useful for checking permissions, comparing tags, or finding overlaps in datasets. You don't need to find all the common elements, you just need to know if an intersection exists.
This guide will teach you the most modern and efficient methods for solving this problem. You will learn how to use the highly readable combination of some() and includes(), and also see a more performant alternative using a Set for larger arrays.
The Core Method: some() with includes()
This is the most modern, concise, and readable way to check for a common element. It combines two powerful array methods.
The logic:
- Use the
Array.prototype.some()method to iterate over the first array (arr1). - For each
elementinarr1, use theArray.prototype.includes()method to check if that element exists in the second array (arr2). - The
some()method will automatically stop and returntrueas soon asincludes()returnstruefor any element. If no common elements are found after checking all of them,some()will returnfalse.
Problem: We need to know if arr1 and arr2 share any common food items.
// Problem: Do these arrays have any elements in common?
const arr1 = ['pizza', 'cake', 'cola'];
const arr2 = ['sushi', 'beer', 'cake'];
const arr3 = ['sushi', 'beer'];Solution:
function haveCommonElement(arr1, arr2) {
return arr1.some(item => arr2.includes(item));
}
// Example Usage:
const arr1 = ['pizza', 'cake', 'cola'];
const arr2 = ['sushi', 'beer', 'cake'];
const arr3 = ['sushi', 'beer'];
console.log(haveCommonElement(arr1, arr2)); // Output: true
console.log(haveCommonElement(arr1, arr3)); // Output: falseA More Performant Method for Large Arrays: Using a Set
The some() with includes() method is very readable, but its performance can degrade with large arrays. The includes() method has to search through arr2 for every single element in arr1, which can be slow.
A more performant approach is to first convert one of the arrays into a Set. Checking for an element's existence in a Set (set.has(item)) is significantly faster than searching an array (array.includes(item)).
Solution:
function haveCommonElement(arr1, arr2) {
const set2 = new Set(arr2);
return arr1.some(item => set2.has(item));
}
// Example Usage:
const arr1 = ['pizza', 'cake', 'cola'];
const arr2 = ['sushi', 'beer', 'cake'];
console.log(haveCommonElement(arr1, arr2)); // Output: trueThe Traditional Method: Using a for...of Loop
Before functional methods like some() were common, this problem was solved with a standard for loop. This approach is still perfectly valid and can be more readable for developers who are less familiar with functional programming.
The logic:
- Loop through each
elementof the first array (arr1). - Inside the loop, check if the second array (
arr2) includes thatelement. - If it does, we have our answer. Return
trueimmediately and exit the loop. - If the loop finishes without finding any common elements, return
false.
Solution:
function haveCommonElement(arr1, arr2) {
for (const item of arr1) {
if (arr2.includes(item)) {
return true; // Found a common element, exit early
}
}
return false; // No common elements were found
}
// Example Usage:
const arr1 = ['pizza', 'cake', 'cola'];
const arr2 = ['sushi', 'beer', 'cake'];
console.log(haveCommonElement(arr1, arr2)); // Output: trueConclusion
Checking if two arrays share at least one element is a common task with several effective solutions in JavaScript.
- The
arr1.some(item => arr2.includes(item))method is the most concise and readable solution, making it an excellent choice for most situations, especially with small to medium-sized arrays. - For large arrays, converting the second array to a
Setfirst (const set2 = new Set(arr2)) and then usingsome()withset2.has()provides better performance. - A traditional
for...ofloop is also a highly efficient and perfectly acceptable solution that is clear and easy to understand.