How to Check if the Given Arrays are Disjoint in Java?


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to check if the given arrays are disjoint. An array is said to be disjoint if two arrays have no element in common i.e. elements in the first array are not equal to elements in the second array.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose the original two arrays are {13, 44, 11, 19, 3} and {7, 12, 1, 5}.

After checking for disjoint in given two array the result will be −

The arrays are disjoint 

Instance-2

Suppose the original two arrays are {22, 4, 15, 9, 53} and {71, 4, 1, 22}

After checking for disjoint in given two array the result will be −

The arrays are not disjoint 

Instance-3

Suppose the original two arrays are {15, 14, 91,9, 1} and {24, 2, 21, 85}

After checking for disjoint in given two array the result will be −

The arrays are disjoint

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Declare boolean to check for disjoint or not.

  • Step 3 − Declare two for loops one inside another.

  • Step 4 − Check if(arr1[i] == arr2[j])

  • Step 5 − if(arr1[i] == arr2[j]) then print array is not disjoint.

  • Step 6 − Else Print Array is disjoint.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length

Where ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm check if the given arrays are disjoint or not.

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args) {
   
      //Declare and initialize two array elements
      int arr1[] = { 15, 14, 91,9, 1 };
      int arr2[] = { 24, 2, 21, 85 };
      
      //declare boolean to check for disjoint or not
      boolean flag = false;
      
      //logic implementation
      for(int i = 0;i<arr1.length;i++){
         for(int j=0;j<arr2.length;j++){
            if(arr1[i] == arr2[j]){
               flag = true;
               break;
            }
         }
      }
      System.out.println("First array is: "+Arrays.toString(arr1));
      System.out.println("Second array is: "+Arrays.toString(arr2));
      
      // print array are disjoint
      if(!flag) {
         System.out.println("The arrays are disjoint");
         return;
      }
      
      // print array are not disjoint
      else {
         System.out.println("The arrays are not disjoint");   
      }
   }
}

Output

First array is: [15, 14, 91, 9, 1]
Second array is: [24, 2, 21, 85]
The arrays are disjoint

Approach-2: By Using User Defined Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the arrays as parameters and inside the method as per the algorithm check if the given arrays are disjoint or not.

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args) {
   
      //Declare and initialize two array elements
      int arr1[] = { 22, 4, 15, 9, 53 };
      int arr2[] = { 71, 4, 1, 22 };
      System.out.println("First array is: "+Arrays.toString(arr1));
      System.out.println("Second array is: "+Arrays.toString(arr2));
      
      //calling user defined method
      func(arr1, arr2);   
   }
   
   //declaring user defined method
   static void func(int []arr1, int []arr2){
   
      //declare boolean to check for disjoint or not
      boolean flag = false;
      
      //logic implementation
      for(int i = 0;i<arr1.length;i++){
         for(int j=0;j<arr2.length;j++){
            if(arr1[i] == arr2[j]){
               flag = true;
               break;
            }
         }
      }
      
      // print array are disjoint
      if(!flag) {
         System.out.println("The arrays are disjoint");
         return;
      }
      
      // print array are not disjoint
      else{
         System.out.println("The arrays are not disjoint");   
      }
   }
}

Output

First array is: [22, 4, 15, 9, 53]
Second array is: [71, 4, 1, 22]
The arrays are not disjoint

In this article, we explored how to check if given arrays are disjoint or not by using Java programming language.

Updated on: 05-Jan-2023

386 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements