2

If I have an array:

var array1=new Array();
array1[0]="two";
array1[1]="three";
array1[2]="four";

And another array:

var array2=new Array();
array2[0]="one";
array2[1]="two";
array2[2]="three";
array2[3]="four";
array2[4]="five";

How can I return a true value that Array1 exists in Array2?

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
steve
  • 849
  • 2
  • 9
  • 15
  • 4
    `var array1=new Array();` Javascript. – Suresh Atta Dec 25 '13 at 18:08
  • 5
    Java is to Javascript as car is to carpet. – SLaks Dec 25 '13 at 18:10
  • As cheese is to cheesecloth. – duffymo Dec 25 '13 at 18:11
  • Since this is infact javascript not java my duplicate post from before does not apply, however this one does [Comparing two arrays in Javascript](http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript) – Jesse Laning Dec 25 '13 at 18:16
  • 2
    @jamolnng — Not a duplicate. The question appears to be asking how to tell if the members of one array are a sub-set of the members of another (not if they are the same set). – Quentin Dec 25 '13 at 18:18
  • Quentin is right. I need to know if an array is a subset of another. – steve Dec 25 '13 at 18:22
  • duplicate: [check if an array contains any elements in another array in Javascript](http://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-elements-in-another-array-in-javascript) – Vland Dec 25 '13 at 18:23

6 Answers6

1

Using set theory we know that A is a subset of B if and only if for every element of A there is an equivalent element in B.

I made a plnkr showing this: http://plnkr.co/edit/9l5QplwWq1m7ZAkXV9PX

Using a simple loop you could do it like this:

function isSubsetOf(arrayA, arrayB) {
  for (var i = 0; i < arrayA.length;i++) {
    var val = arrayA[i];
    if (arrayB.indexOf(val) !== -1) {
      continue;
    } else {
      return false;
    }
  }
  return true;
}
Neikos
  • 1,840
  • 1
  • 22
  • 35
1

Try like this:

var arr1= ["two", "three"];
var arr2= ["two", "three", "four", "five"];

var isarraySubset = arr1.every(function(val) { return arr2.indexOf(val) >= 0; }));
console.log(isarraySubset ); // true

Note:-

This solution will work if your supported browsers are of ECMA-262 standard.

According MDN,

"Tests whether all elements in the array pass the test implemented by the provided function."

Here is the signature of the method:

array.every(callback[, thisObject])

where callback is a function to test for each element, and thisObject refers to the object that we are testing array against

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

convert array to list using Arrays.asList(array2) and same for array1 then call collections contains method

Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22
0
var checkArray = [];
for ( var i; i<array1.length; i++ ){
     if ( array2.indexOf( array1[i] ) !== -1 ) {
              checkArray.push(array1[i]);
      }
}
console.log("duplicate values : "+checkArray)// duplicate values : two,three,four

this may be not the most performant way ( i didnt test it, its pseudo-code) but at the end checkArray should contain all values of array1 that are in array2

john Smith
  • 17,409
  • 11
  • 76
  • 117
0

If your requirement is to return true only if the sequence [two, three, four] is in array2, here is a fast but not 100% correct method:

var sep = 'zzz_some_highly_unlikely_occurring_string_zzz';
array2.join(sep).indexOf( array1.join(sep) );
Ksthawma
  • 1,257
  • 1
  • 16
  • 28
  • Also I don't know if non-string array values will work in this way. Any object can be made .toString() but will it still work? – Ksthawma Dec 25 '13 at 18:34
0
  var a1="";
 for(var tmp:array1)
     a1+=tmp;
 var a2="";
 for(var tmp:array2)
      a2+=tmp;

Now check if a1 exists in a2

  a2.indexOf(a1)>-1

Will return true if array1 is there in array2