Check If One Array Is a Subset Of Another

An array is said to be a subset of another when all its elements are fully contained in the other.

Design a class named ‘Subset’ to test whether x[] is a subset of y[] – both storing integers.

Class name: Subset
Instance variables:
a[]: integer array
nl: size of the array
Member functions/methods:
Subset(int): parameterized constructor to assign value to nl and create the array accordingly.
static int isValid(int, int): validate whether size of the array x[] <= size of the array y[].
void input(): to accept array elements in desired arrays. Inputs are in random order without duplicate elements.
void display(): to display array elements of the desired arrays.
boolean combine(Subset): to test whether the argument object is a subset of the current (this) object without sorting and return 1 for true and 0 for false.

Also include the main() method to create objects accordingly to enable the task.

import java.io.*;
class Subset{
    int a[];
    int nl;
    public Subset(int size){
        nl = size;
        a = new int[nl];
    }
    public static int isValid(int x[], int y[]){
        if(x.length <= y.length)
            return 1;
        return 0;
    }
    public void input()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for(int i = 0; i < a.length; i++){
            boolean duplicate = false; 
            do{ 
                duplicate = false; 
                System.out.print("Enter element " + (i + 1) + ": "); 
                a[i] = Integer.parseInt(br.readLine()); 
                for(int j = i - 1; j >= 0; j--){
                    if(a[j] == a[i]){
                        duplicate = true;
                        break;
                    }
                }
            }while(duplicate);
        }
    }
    public void display(){
        for(int i = 0; i < nl; i++)
            System.out.print(a[i] + "\t");
        System.out.println();
    }
    public boolean combine(Subset x){
        int count = 0;
        if(isValid(x.a, this.a) == 0)
            return false;
        for(int i = 0; i < x.nl; i++){
            for(int j = 0; j < this.nl; j++){
                if(x.a[i] == this.a[j]){
                    count++;
                    break;
                }
            }
        }
        return count == x.nl;
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("x array size: ");
        int size1 = Integer.parseInt(br.readLine());
        System.out.print("y array size: ");
        int size2 = Integer.parseInt(br.readLine());
        Subset x = new Subset(size1);
        Subset y = new Subset(size2);
        System.out.println("Enter x array elements:");
        x.input();
        System.out.println("Enter y array elements:");
        y.input();
        System.out.println("x array elements:");
        x.display();
        System.out.println("y array elements:");
        y.display();
        if(y.combine(x))
            System.out.println("x is a subset of y.");
        else
            System.out.println("x is not a subset of y.");
    }
}

Leave a Reply

%d bloggers like this: