JavaScript - Check if Two Arrays are Disjoint Last Updated : 17 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Here are some approaches to determine if two arrays share any common item in JavaScript1. Using Loops - SimpleThe simplest way to find common items is by using nested loops to compare each item in one array (a1) with every item in the second array (a2). This is great for smaller arrays, but less efficient for larger datasets. JavaScript const a1 = [1, 2, 3]; const a2 = [4, 5, 3]; let hasCommonItem = false; for (let i = 0; i < a1.length; i++) { for (let j = 0; j < a2.length; j++) { if (a1[i] === a2[j]) { hasCommonItem = true; break; } } } console.log(hasCommonItem); Outputtrue 3. Using Set and some() - EfficientFor larger arrays, converting one array (a2) into a Set and using some() to check items in a1 against the Set reduces time complexity as Set provides constant-time lookups. JavaScript const a1 = [1, 2, 3]; const a2 = [4, 5, 3]; const set = new Set(a2); const hasCommonItem = a1.some(item => set.has(item)); console.log(hasCommonItem); Outputtrue 2. Using some() and includes()The some() method, when combined with includes(), provides a more simpler way to check if any item in a1 exists in a2. JavaScript const a1 = [1, 2, 3]; const a2 = [4, 5, 3]; const hasCommonItem = a1.some(item => a2.includes(item)); console.log(hasCommonItem); Outputtrue 4. Using filter() to Find Common ItemsThe filter() method can be used to get an array of items present in both a1 and a2. If the result has elements, then a1 and a2 share common items. JavaScript const a1 = [1, 2, 3]; const a2 = [4, 3, 5]; const commonItems = a1.filter(item => a2.includes(item)); console.log(commonItems); console.log(commonItems.length > 0); Output[ 3 ] true 5. Using Set IntersectionCreating a Set intersection is efficient when both arrays are converted into Sets. We then filter items from a1 that are in a2, resulting in the intersection of the two sets. JavaScript const a1 = [1, 2, 3]; const a2 = [3, 4, 5]; const intersection = new Set(a1.filter(item => new Set(a2).has(item))); console.log(intersection.size > 0); Outputtrue Importance of Finding Common Items Between ArraysFinding common items between arrays is crucial forData Comparison: Quickly verifying overlaps between data lists.Conditional Processing: Executing code based on shared elements.Performance: Optimizing large data comparisons by choosing efficient methods like Set. Comment More infoAdvertise with us Next Article JavaScript - Check if Two Arrays are Disjoint A anuupadhyay Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads JavaScript- Arrays are Equal or Not These are the following approaches to compare two arrays in JavaScript:1. Using the JSON.stringify() MethodJavaScript provides a function JSON.stringify() method in order to convert an object whether or array into a JSON string. By converting it into JSON strings, we can directly check if the string 4 min read Difference Between Two Arrays in JavaScript? These are the following ways to Get the Difference Between Two Arrays in JavaScript:1. Using the filter() and includes() Methods - Mostly UsedWe can use the filter() method on the first array and check if each item is not present in the second array using the includes() method. The resulting array c 3 min read Javascript Program for Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[] 4 min read How to get symmetric difference between two arrays in JavaScript ? In this article, we will see how to get the symmetric difference between two arrays using JavaScript. In Mathematics the symmetric difference between two sets A and B is represented as A ΠB = (A - B) ⪠(B - A) It is defined as a set of all elements that are present in either set A or set B but not 6 min read PHP | Find Intersection of two arrays You are given two arrays of n-elements each. You have to find all the common elements of both elements, without using any loop in php and print the resulting array of common elements. Example: Input : array1[] = {3, 5, 2, 7, 9}, array2[] = {4, 3, 2, 7, 8} Output : array ( [0] => 3, [1] => 2, [ 2 min read Like