In this article, we are given two arrays containing array elements and the task is to check if two arrays contain any common elements then it returns True otherwise returns False.
Examples:
Input: array1 = ['a', 'b', 'c', 'd', 'e']
array2 = ['f', 'g', 'c']
Output: true
Input: array1 = ['x', 'y', 'w', 'z']
array2 = ['m', 'n', 'k']
Output: false
There are many methods to solve this problem in JavaScript some of them are discussed below.
Methods to check the common items in arrays:
Method 1: Brute Force approach: using JavaScript loops
- Compare each and every item from the first array to each and every item of the second array.
- Loop through array1 and iterate it from beginning to end.
- Loop through array2 and iterate it from beginning to end.
- Compare each and every item from array1 to array2 and if it finds any common item then return true otherwise return false.
Example: In this example, we will be using the above approach to find if two arrays contain any common item in Javascript.
javascript
let array1 = [ 'a' , 'b' , 'c' , 'd' ];
let array2 = [ 'k' , 'x' , 'z' ];
function findCommonElement(array1, array2) {
for (let i = 0; i < array1.length; i++) {
for (let j = 0; j < array2.length; j++) {
if (array1[i] === array2[j]) {
return true ;
}
}
}
return false ;
}
console.log(findCommonElement(array1, array2))
|
- Create an empty object and loop through the first array.
- Check if the elements from the first array exist in the object or not. If it doesn’t exist then assign properties === elements in the array.
- Loop through the second array and check if elements in the second array exist on created object.
- If an element exists then return true else return false.
Example: In this example, we will be using the above approach to find if two arrays contain any common item in Javascript.
javascript
let array1 = [ 'a' , 'd' , 'm' , 'x' ];
let array2 = [ 'p' , 'y' , 'k' ];
function findCommonElements2(arr1, arr2) {
let obj = {};
for (let i = 0; i < arr1.length; i++) {
if (!obj[arr1[i]]) {
let element = arr1[i];
obj[element] = true ;
}
}
for (let j = 0; j < arr2.length; j++) {
if (obj[arr2[j]]) {
return true ;
}
}
return false ;
}
console.log(findCommonElements2(array1, array2))
|
- Use the inbuilt ES6 function some() to iterate through each and every element of the first array and to test the array.
- Use the inbuilt function includes() with the second array to check if an element exists in the first array or not.
- If an element exists then return true else return false
Example: In this example, we will be using the above approach to find if two arrays contain any common item in Javascript.
javascript
let array1 = [ 'a' , 'b' , 'x' , 'z' ];
let array2 = [ 'k' , 'x' , 'c' ]
function findCommonElements3(arr1, arr2) {
return arr1.some(item => arr2.includes(item))
}
console.log(findCommonElements3(array1, array2))
|
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.