How to find if two arrays contain any common item in Javascript? - GeeksforGeeks

19 Oct, 2021
Given two arrays containing array elements and the task is to check if two arrays contain any common elements then it returns True otherwise return 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.Method 1: Brute Force approach  
Compare each and every item from the first array to each and every item of second array.
Loop through array1 and iterate it from beginning to the end.
Loop through array2 and iterate it from beginning to the end.
Compare each and every item from array1 to array2 and if it finds any common item then return true otherwise return false.
Example:  
javascript
<script> // Declare two arrayconst array1 = ['a', 'b', 'c', 'd'];const array2 = ['k', 'x', 'z']; // Function definition with passing two arraysfunction findCommonElement(array1, array2) {         // Loop for array1    for(let i = 0; i < array1.length; i++) {                 // Loop for array2        for(let j = 0; j < array2.length; j++) {                         // Compare the element of each and            // every element from both of the            // arrays            if(array1[i] === array2[j]) {                             // Return if common element found                return true;            }        }    }         // Return if no common element exist    return false;} document.write(findCommonElement(array1, array2))</script>                   
Output:  
false
Time Complexity: O(M * N)Method 2:  
Create an empty object and loop through 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 second array and check if elements in the second array exists on created object.
If element exist then return true else return false.
Example:  
javascript
<script> // Declare Two arrayconst array1 = ['a', 'd', 'm', 'x'];const array2 = ['p', 'y', 'k']; // Function callfunction findCommonElements2(arr1, arr2) {         // Create an empty object    let obj = {};                 // Loop through the first array        for (let i = 0; i < arr1.length; i++) {                         // Check if element from first array            // already exist in object or not            if(!obj[arr1[i]]) {                             // If it doesn't exist assign the                // properties equals to the                // elements in the array                const element = arr1[i];                obj[element] = true;            }        }                 // Loop through the second array        for (let j = 0; j < arr2.length ; j++) {                 // Check elements from second array exist        // in the created object or not        if(obj[arr2[j]]) {            return true;        }    }    return false;} document.write(findCommonElements2(array1, array2))</script>                   
Output:  
false
Time Complexity: O(M + N)Method 3:  
Use the inbuilt ES6 function some() to iterate through each and every element of first array and to test the array.
Use the inbuilt function includes() with second array to check if element exist in the first array or not.
If element exist then return true else return false
 
javascript
<script> // Declare two arrayconst array1= ['a', 'b', 'x', 'z'];const array2= ['k', 'x', 'c'] // Iterate through each element in the// first array and if some of them// include the elements in the second// array then return true.function findCommonElements3(arr1, arr2) {    return arr1.some(item => arr2.includes(item))} document.write(findCommonElements3(array1, array2))</script>                   
Output:  
true
 
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.
 
kalrap615
javascript-array
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript