Subset of array java – Java Program to Check if One Array is Subset of Another Array or Not

Subset of array java: In the previous article, we have seen Java Program to Check if Two Arrays are Equal or Not

In this article we will see if one array is subset of another array or not using Java programming language.

Java Program to Check if All the Elements of an Array is Present in Another Array or Not

Java subset of array: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to check one array is subset of another array or not.

Method-1: Java Program to Check if One Array is Subset of Another Array or Not By Static Initialization of Array Elements

Approach:

  1. Declare and initialize two arrays.
  2. First, if first array length is less than length of second array then second array can not be subset of first array.
  3. Check if the second array is subset of first array or not by iterating and matching each element of both the array.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // declaring two arrays
        int[] arr1 = { 1, 2, 6, 7, 4, 3, 2 };
        System.out.println("First array is : ");
        printArray(arr1);
        
        int[] arr2 = { 1, 6, 7 };
        System.out.println("Second array is : ");
        printArray(arr2);
        
        // calling isSubset method
        if(isSubset(arr1, arr2))
            System.out.println("Subset");
        else
            System.out.println("Not Subset");

    }

    static boolean isSubset(int[] arr1, int[] arr2) 
    {
        //If length of two arrays are not same return false
        if (arr1.length < arr2.length) 
            return false;
         
         int j=0;   
        for (int i = 0; i < arr2.length; i++) 
        {
            for (j = 0; j < arr1.length; j++)
                if (arr2[i] == arr1[j])
                    break;

            // Checking if the above inner loop was not broken at all,
            // then arr2 isn't a subset of arr1
            if (j == arr1.length)
                return false;
        }
        // if the inner loop was broken, then arr2 is a subset of arr1
        return true;
    }
    
    //printArray() method to print the array 
    static void printArray(int[] arr) 
    { 
        // printing array 
        for (int i=0; i<arr.length; i++) 
        { 
            System.out.print(arr[i] + " "); 
        } 
        System.out.println("");
    }
}
Output:

First array is : 
1 2 6 7 4 3 2 
Second array is : 
1 6 7 
Subset

Method-2: Java Program to Check if One Array is Subset of Another Array or Not By Dynamic Initialization of Array Elements

Approach:

  1. Take input of two arrays.
  2. First, if first array length is less than length of second array then second array can not be subset of first array.
  3. Check if the second array is subset of first array or not by iterating and matching each element of both the array.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        // creating scanner class object
        Scanner sc = new Scanner(System.in);
        
        // taking input for size of arr1
        System.out.println("Enter size of arr1: ");
        int m = sc.nextInt();
        int[] arr1 = new int[m];
        
        // asking user for input for arr1 elements
        System.out.println("Enter elements of arr1: ");
        for (int i = 0; i < m; i++) 
        {
            arr1[i] = sc.nextInt();
        }

        // taking input for size of arr2
        System.out.println("Enter size of arr2: ");
        int n = sc.nextInt();
        
        // asking user for input for arr2 elements
        System.out.println("Enter elements of arr2: ");
        int[] arr2 = new int[n];
        for (int i = 0; i < n; i++) 
        {
            arr2[i] = sc.nextInt();
        }

        System.out.println("First array is : ");
        printArray(arr1);
        
        System.out.println("Second array is : ");
        printArray(arr2);
        
        // calling isSubset method
        if(isSubset(arr1, arr2))
            System.out.println("Subset");
        else
            System.out.println("Not Subset");

    }

    static boolean isSubset(int[] arr1, int[] arr2) 
    {
        //If length of two arrays are not same return false
        if (arr1.length < arr2.length) 
            return false;
         
         int j=0;   
        for (int i = 0; i < arr2.length; i++) 
        {
            for (j = 0; j < arr1.length; j++)
                if (arr2[i] == arr1[j])
                    break;

            // Checking if the above inner loop was not broken at all,
            // then arr2 isn't a subset of arr1
            if (j == arr1.length)
                return false;
        }
        // if the inner loop was broken, then arr2 is a subset of arr1
        return true;
    }
    
    //printArray() method to print the array 
    static void printArray(int[] arr) 
    { 
        // printing array 
        for (int i=0; i<arr.length; i++) 
        { 
            System.out.print(arr[i] + " "); 
        } 
        System.out.println("");
    }
}
Output:

Case-1

Enter size of arr1: 8
Enter elements of arr1: 1 2 3 4 5 6 7 8
Enter size of arr2: 5
Enter elements of arr2: 1 2 3 4 5
First array is : 
1 2 3 4 5 6 7 8 
Second array is : 
1 2 3 4 5 
Subset

Case-2

Enter size of arr1: 8
Enter elements of arr1: 1 2 3 4 5 6 7 8
Enter size of arr2: 5
Enter elements of arr2: 1 2 3 4 9
First array is : 
1 2 3 4 5 6 7 8 
Second array is : 
1 2 3 4 9
Not Subset

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: