How to check whether an array is subset of another array in Java

1 Answer

0 votes
public class MyClass {
    static boolean isSubset(int []arr1, int []arr2) {
        int size1 = arr1.length;
        int size2 = arr2.length;
        int j;
        for (int i = 0; i < size2; i++) {
            for (j = 0; j < size1; j++) {
                if (arr2[i] == arr1[j])
                    break;
            }
            if (j == size1)
                return false;
        }
        return true;
    }
    public static void main(String args[]) {
        int []arr1 = { 5, 1, 8, 12, 40, 7, 9, 100 };
        int []arr2 = { 8, 40, 9, 1 };

        if (isSubset(arr1, arr2))
            System.out.println("yes");
        else
            System.out.println("no");
    }
}



  
/*
run:
  
yes
  
*/

 



answered Dec 21, 2021 by avibootz

Related questions

2 answers 119 views
1 answer 79 views
1 answer 81 views
1 answer 88 views
1 answer 81 views
...