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 !");
}

2 comments:

  1. Amazing Article, thank you!. I am very glad to read your informative & practical blog. Kindly keep updating your blog. Java Developer is a wonderful career for IT students.To start Dream Career to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India .

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...