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?

7
  • 4
    var array1=new Array(); Javascript. Commented Dec 25, 2013 at 18:08
  • 5
    Java is to Javascript as car is to carpet. Commented Dec 25, 2013 at 18:10
  • As cheese is to cheesecloth. Commented Dec 25, 2013 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 Commented Dec 25, 2013 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). Commented Dec 25, 2013 at 18:18

6 Answers 6

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

2 Comments

That's Java. The question is asking about JavaScript.
previously it was tagged as java
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) );

1 Comment

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?
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.