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 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
Javascript Program to Print uncommon elements from two sorted arrays Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples :Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50}Output : 10 25 40 50We do not print 20 and 30 as theseelements are pr
2 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
Java Program to Compare two Boolean Arrays Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the ar
3 min read
Assert Two Lists for Equality Ignoring Order in Java In Java, comparing two lists for equality typically checks both the content and the order of the elements using the equals() method. There are many cases where the order of the elements does not matter, and we only want to ensure that both lists contain the same elements, regardless of their order.
6 min read
Check if given two Arrays are equal (using Map) Given two arrays, A[] and B[], the task is to check if they are equal or not. Arrays are considered equal if any permutation of array B equates to array A. Examples: Input: A[] = [2, 4, 5, 7, 5, 6] and B[] = [4, 2, 5, 5, 6, 7]Output: YesExplanation: All the elements in array A are present in array B
6 min read