A
A
AskJs2018-02-26 18:06:52
JavaScript
AskJs, 2018-02-26 18:06:52

How to check an array?

Hello.
How to check an array for the presence of elements in this way:

var a = ["as", "sa", "ds"];
myFunc("sa", "as", "ds"); -> 1
myFunc("sa", "as", "k"); -> 0
myFunc("sa", "as", "ds", "sa", "as", "ds"); -> 2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Tsvetkov, 2018-02-26
@AskJs

var a = ["as", "sa", "ds"];
function myFunc() {
  var results = [];
  for(var i in a) {
    results.push(0);
  }
  for(var i = 0; i < arguments.length; i++) {
    var index = a.indexOf(arguments[i]);
    if(index != -1) {
      results[index] += 1;
    }
  }
  return Math.min.apply(Math, results);
}

myFunc("sa", "as", "ds"); // 1
myFunc("sa", "as", "k"); // 0
myFunc("sa", "as", "ds", "sa", "as", "ds"); // 2
myFunc("sa", "as", "ds", "sa", "as", "ks");// 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question