Class 11 Class 12

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.

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(;
        for(int i = 0; i < a.length; i++){
            boolean duplicate = false; 
                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;
    public void display(){
        for(int i = 0; i < nl; i++)
            System.out.print(a[i] + "\t");
    public boolean combine(Subset x){
        int count = 0;
        if(isValid(x.a, this.a) == 0)
            return false;
        for(int i = 0; i <; i++){
            for(int j = 0; j <; j++){
                if(x.a[i] == this.a[j]){
        return count ==;
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(;
        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:");
        System.out.println("Enter y array elements:");
        System.out.println("x array elements:");
        System.out.println("y array elements:");
            System.out.println("x is a subset of y.");
            System.out.println("x is not a subset of y.");

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.