Possible Duplicate:
Check whether an array is a subset of another
I have array a = { 1,4,3,5,1,2,3,6 } and array b= { 1,2,3 }.
How can I check if array b elements appear anywhere in array a? Any help will be appreciated. Thanks.
Possible Duplicate:
Check whether an array is a subset of another
I have array a = { 1,4,3,5,1,2,3,6 } and array b= { 1,2,3 }.
How can I check if array b elements appear anywhere in array a? Any help will be appreciated. Thanks.
The easiest way is obviously to use the built-in functions. However, Java only has built-in functions to do this for Collection types. If the arrays are not of primitive types, you can do this:
if (Arrays.asList(a).containsAll(Arrays.asList(b))) {
//...
}
If it's a primitive array (int[], etc) this is still the easiest approach, you just need to convert the array into a list manually or using a third-party library. Guava has functions for this:
if ( Ints.asList(a).containsAll(Ints.asList(b)) ) {
//...
}
Use Apache Commons Lang:
for (Object element:b) {
if (ArrayUtils.contains(a,element) == false) return false;
}
return true;