![]() |
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] Table of Content 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 Example: Implementation of program to find whether arrays are disjoint or not using Array.some() method
Output Are 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 SetThe 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 Example: Implementation of program to find whether arrays are disjoint or not using Set
Output Are 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 LoopsIn 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 Example: Implementation of program to find whether arrays are disjoint or not using Nested Loops
Output Are 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) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |