Check One Array is Subset of Another Array 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 whether one array is subset of another array. An array is a subset of another array if all the elements of subarray is present in the given 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 array is array1 {33, 51, 5, 31, 9, 4, 3}
The sub array is array2 {51, 9, 33, 3}
After checking if original array contains all elements of sub array, result will be:
array2 is a subset of array1

Instance-2

Suppose the original array is array1 {14, 11, 33, 2, 9, 1}
The sub array is array2 {11, 2, 7, 1}
After checking if original array contains all elements of sub array, result will be:
array2 is not a subset of array1

Instance-3

Suppose the original array is array1 {8, 28, 41, 3, 29, 10}
The sub array is array2 {28, 10}
Hence array2 is a sub array of array1

Algorithm

Algorithm-1 (By Using 2 for loops)

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Implement the logic for multiple approaches.

  • Step 3 − initialise two for loops and check if elements of inner for loop matches outer for loop.

  • Step 4 − Print the result.

Algorithm-2 (By using HashList)

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Implement the logic for multiple approaches.

  • Step 3 − Initialise hashset and check if elements of subarray are there in the original array or not by “.contains(arr1[i])”.

  • Step 4 − Print the result.

Algorithm-3 (By using List)

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Implement the logic for multiple approaches.

  • Step 3 − Initialise array list and check if sub array elements are there in original array or not.

  • Step 4 − Print the result.

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 2 for loops

  • By Using Hashing

  • Using List.contains() Method

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

Approach-1: By Using 2 for Loops

Initialise two for loops and check if elements of inner for loop matches outer for loop. Then as per the algorithm check whether one array is a subset of another array.

Example

public class Main {
   public static void main(String args[]) {
      int array1[] = { 33, 51, 5, 31, 9, 4, 3 };
      int array2[] = { 51, 9, 33, 3 };
      int x = array1.length;
      int y = array2.length;subset(array1, array2, x, y);
      if (subset(array1, array2, x, y)) {
         System.out.print("array 2 is a subset of array 1");
      } else {
         System.out.print("array 2 is not a subset of array 1");
      }
   }

   //user defined method to check if array 2 is present in array 1
   static boolean subset(int array1[], int array2[], int x, int y) {
      int i, j = 0;
      for (i = 0; i < y; i++) {
         for (j = 0; j < x; j++)
            if (array2[i] == array1[j])
               break;
         /* return false when arr2[i] is not present in arr1[] */
         if (j == x)
            return false;
      }
      /* return true when all elements of arr2[] are present in arr1[] */
      return true;
   }
}

Output

array 2 is a subset of array 1

Approach-2: By Using Hashing

Initialise hashset and check if elements of subarray are there in the original array or not by “.contains(arr1[i]”. Then as per the algorithm check whether one array is a subset of another array.

Example

import java.util.HashSet;
public class Main {
   public static void main(String[] args) {

      //declaring and initialising arrays
      int arr1[] = { 14, 11, 33, 2, 9, 1 };
      int arr2[] = { 11, 2, 7, 1 };
      
      //getting the length of the arrray
      int x = arr1.length;
      int y = arr2.length;
      if (subset(arr1, arr2, x, y))
         System.out.println("array 2 is a subset of array 1 ");
      else
         System.out.println(
      "array 2 is not a subset of array 1");
   }
   /* Return true if arr2[] is a subset of arr1[] */
   static boolean subset(int arr1[], int arr2[], int x, int y) {
   
      //declaring hashset
      HashSet<Integer> hashset = new HashSet<>();
      
      // hashset stores all the values of arr1
      for (int i = 0; i < x; i++) {
         if (!hashset.contains(arr1[i]))
            hashset.add(arr1[i]);
      }
      
      // for loop to check if all elements of arr2 also lies in arr1
      for (int i = 0; i < y; i++) {
         if (!hashset.contains(arr2[i]))
            /* return false when arr2[i] is not present in arr1[] */
         return false;
      }
      /* return true when all elements of arr2[] are present in arr1[] */
      return true;
   }
}

Output

array 2 is not a subset of array 1

Approach-3: By Using List.contains() Method

Initialise array list and check if sub-array elements are there in the original array or not. Then as per the algorithm check whether one array is a subset of another array.

Example

import java.util.*;
public class Main {
   public static void main(String[] args) {
   
      //declaring and initialising arrays
      Integer arr1[] = { 8, 28, 41, 3, 29, 10 };
      Integer arr2[] = { 28, 10};
      
      //printing the arrays
      System.out.println("Original array is " + Arrays.toString(arr1));
      System.out.println("The sub array is " + Arrays.toString(arr2));
      
      //converting array to array list
      List<Integer> arr = new ArrayList<Integer>(Arrays.asList(arr1));
      
      // use contains() to check if the element 28 is present or not
      boolean ans = arr.contains(28);
      
      //if 28 is present then print successful message
      if (ans)
         System.out.println("The array 1 contains 28");
      else
         System.out.println("The array 1 does not contains 28");
      
      // use contains() to check if the element 10 is present or not
      ans = arr.contains(10);
      
      //if 10 is present then print successful message
      if (ans)
         System.out.println("The array 1 contains 10");
      else
         System.out.println("The array 1 does not contains 10");
      
      //print all elements of array 2 is in array 1
      System.out.println("Hence array 2 is a sub array of array 1");
   }
}

Output

Original array is [8, 28, 41, 3, 29, 10]
The sub array is [28, 10]
The array 1 contains 28
The array 1 contains 10
Hence array 2 is a sub array of array 1

In this article, we explored how to check whether one array is a subset of another array by using Java programming language.

Updated on: 31-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements