D
D
diasflack2013-11-19 10:27:49
JavaScript
diasflack, 2013-11-19 10:27:49

What is the JavaScript way to check if one array is completely contained in another without using a double loop?

Advise a way to check the complete occurrence of one array in another, regardless of the order of the elements in JavaScript without using a double loop - actually the question.
While I have this

var equalTags = 0;
for(var a=0; a<tag.length; a++) {
  for(var b=0; b<tagsOff.length; b++) {
    if (tag[a]==tagsOff[b]) {
      equalTags ++;
     }	
  }
  if (equalTags == tag.length) {
    console.log(i+' - off - '+equalTags);
  } else {
    console.log(i+' - on - '+equalTags);
}
}

I would like to get rid of the double loop and loops in general, if there is a more elegant solution, I will be grateful

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
ramntry, 2013-11-19
@diasflack

If @egor_nullptr understood you correctly, and you need to check that the array b contains at least one instance of each element of the array a, then I propose this solution:

Array.prototype.hasAll = function(a) {
    var hash = this.reduce(function(acc, i) { acc[i] = true; return acc; }, {});
    return a.every(function(i) { return i in hash; });
};

Compared to @egor_nullptr 's solution, it's asymptotically faster: O(n + m) vs. O(n * m), where n, m are the sizes of arrays a, b. With n = m = 100,000 on my machine, my version is 200 times faster. Tested like this .
Do you know what my JavaScript development experience is like? I've been programming on it for 40 minutes, along with Mozilla 's JavaScript Reference . My main language is C++. And I strongly advise you to get to some book on algorithms and data structures (for example, to " Introduction to Algorithms ". It is in Russian, the best translation, in my opinion, is from MCNMO). It's helpful, I assure you.

E
egor_nullptr, 2013-11-19
@egor_nullptr

Array.prototype.diff = function(a) {
    return this.filter(function(i) {return !(a.indexOf(i) > -1);});
};

[5,3,4].diff([1,2,3,4,5,6])  
// => [], и это означает полное вхождение

N
NeX, 2013-11-19
@NeX

If undersore is connected, then, for example, like this:
http://underscorejs.ru/#without

// Вернет массив a без элементов из массива a
if (_.without.apply(this, b.unshift(a))) {
}

A
Artyushov, 2013-11-19
@Artyushov

If it is performance that is important, then the problem is similar to the problem of checking the occurrence of a substring. To do this, you can use the Knuth-Morris-Pratt or Rabin-Karp algorithms.

S
Sergey, 2013-11-19
Protko @Fesor

Usually I do such checks through array intersections (or they gave you the diff function). It won't work without nested loops, because in fact both indexOf and array.filter are loops. Unless you can interrupt the execution of the loop as soon as there is an element that does not fall into another array.

function isSame(a, b) {
    if (a.length > b.length) {
         b = [a, a = b][0]; //swap
    }

    for(var i = 0, length = a.length;i<length;i++) {
         if (-1 === b.indexOf(a[i])) {
              return false;
         }
    }
    
    return true;
}

something like that. But I like the diff option better.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question