JavaScript Program to Find if Arrays are Disjoint or Not
Last Updated :
15 May, 2024
Given two arrays, Our task is to check if the given two arrays are disjoint or not. Disjoint arrays in JavaScript are arrays that have no elements in common
Input: arr1[] = [12, 34, 11, 9, 3]
arr2[] = [2, 1, 3, 5]
Output: Not Disjoint
3 is common in two sets.
Input: arr1[] = [12, 34, 11, 9, 3]
arr2[] = [7, 2, 1, 5]
Output: Yes, Disjoint
There is no common element in two sets.
Use array.some()
The array.some() method checks if any element in one array exists in the other array.If any common element is found, it returns false
, indicating that the arrays are not disjoint. Otherwise, it returns true
.
Example: Implementation of program to find whether arrays are disjoint or not using Array.some() method
JavaScript
const disjoint = (a, b) => !a.some(e => b.includes(e));
const arr1 = [1, 2, 3, 4];
const arr2 = [5, 6, 7, 8];
console.log("Are arrays disjoint:", disjoint(arr1, arr2));
OutputAre arrays disjoint: true
Time Complexity: O(n*m),where n is the length of arr1 and m is the length of arr2.
Auxiliary Space: O(1)
Using Set
The Set method converts one array into a Set and then iterates through the other array to check if any element exists in the Set. If any common element is found, it returns false
, indicating that the arrays are not disjoint. Otherwise, it returns true
.
Example: Implementation of program to find whether arrays are disjoint or not using Set
JavaScript
const disjoint = (a, b) => {
const set = new Set(a);
for (const e of b) {
if (set.has(e)) return false;
}
return true;
};
const arr1 = [1, 2, 3, 4];
const arr2 = [5, 6, 7, 8];
console.log("Are arrays disjoint:", disjoint(arr1, arr2));
OutputAre arrays disjoint: true
Time Complexity: O(n+m),where n is the length of arr1 and m is the length of arr2.
Auxiliary Space: O(n)
Using Nested Loops
In this approach we use Nested Loop to iterating through each element of one array and comparing it with every element of the other array. If any common element is found, it returns false
, indicating that the arrays are not disjoint. Otherwise, it returns true
.
Example: Implementation of program to find whether arrays are disjoint or not using Nested Loops
JavaScript
const disjoint = (a, b) => {
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < b.length; j++) {
if (a[i] === b[j]) return false;
}
}
return true;
};
const arr1 = [1, 2, 3, 4];
const arr2 = [5, 6, 7, 8];
console.log("Are arrays disjoint:", disjoint(arr1, arr2));
OutputAre arrays disjoint: true
Time Complexity: O(n*m),where n is the length of arr1 and m is the length of arr2.
Auxiliary Space: O(1)
Similar Reads
JavaScript Program to Check if an Array Contains only Unique Values In this article, we are given an array, Our task is to find whether the elements in an array are unique or not.Examples:Input 1: 7,8,1,5,9 Output: true Input2: 7,8,1,5,5 Output: falseIn Input 1, elements 7,8,1,5,9 are distinct from each other and they were unique, there was no repetition of elements
4 min read
JavaScript Program to Find the Distance Value between Two Arrays We will be given two integer arrays and an integer d. The task is to calculate the distance between the two given arrays based on the given integer value. The distance value will be calculated as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <=
3 min read
JavaScript Program to Find Union and Intersection of Two Unsorted Arrays In this article, we will learn how to find the Union and Intersection of two arrays. When an array contains all the elements that are present in both of the arrays, it is called a union. On the other hand, if an array has only those elements that are common in both of the arrays, then it is called a
13 min read
JavaScript Program to Count Unequal Element Pairs from the Given Array Unequal element pairs in an array refer to pairs of distinct elements within the array that have different values. These pairs consist of two elements that are not equal to each other, highlighting their inequality when compared in the context of the array's values.Examples:Input: arr[] = {6, 5, 2,
4 min read
JavaScript - Check if Two Arrays are Disjoint 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 eff
2 min read
Java Program to Check if two Arrays are Equal or not In Java, comparing two arrays means checking if they have the same length and identical elements. This checking process ensures that both arrays are equivalent in terms of content and structure.Example:In Java, the simplest way to check if two arrays are equal in Java is by using the built-in Arrays
3 min read