Sunday, 22 May 2016

How to check an array is subset of another array?

Check an array is subset of another array

Scenario#
Check that all element of first array present in second array.

Solution#
Apply Brute force algorithm to check the occurrence of each element of  first array into second array.

Solution# Java method
First, convert arrays into list using arrayList().
Use containsAll() method to check.


Integer[] array1 = {1,2,3};
Integer[] array2 = { 2,3 };

List list1 = Arrays.asList(array1);
List list2 = Arrays.asList(array2);

if(list1.containsAll(list2)) {
      System.out.println("Yo Yo ! They are there !");
}

1 comment:

Related Posts Plugin for WordPress, Blogger...