
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Check if two given sets are disjoint?
- Java Program to Check if two Arrays are Equal or not
- Java program to check if two given matrices are identical
- How To Check if a Triangle is Valid or Not When Sides are Given in Java?
- How to check if two strings are equal in Java?
- How To Check If Three Points are Collinear in Java?
- Check if values of two arrays are the same/equal in JavaScript
- How to check if a given character is a number/letter in Java?
- How To Check if a Given Point Lies Inside a Rectangle in Java?
- How to check if two dates are equal in Java 8?
- Swift Program to Check if Two Arrays Are Equal or Not
- Golang Program to Check If Two Arrays are Equal or Not
- How to check a String for palindrome using arrays in java?
- Java Program to Check if An Array Contains the Given Value
- Check if a given key exists in Java HashMap
