简体   繁体   English

检查数组是否包含2个不同值的最快方法?

[英]Fastest way to check if array contains 2 different values?

Consider the following arrays: 考虑以下数组:

['a', 'b', 'a'] //method should return true
['a', 'b', 'c'] //method should return true
['a', 'c', 'c'] //method should return false

I want to write a method that most efficiently checks to see if both 'a' and 'b' exist in the array. 我想编写一种最有效地检查数组中是否同时存在“ a”和“ b”的方法。 I know I can do this in a simple for loop 我知道我可以在一个简单的for循环中做到这一点

let a_counter = 0;
let b_counter = 0;
for (let i = 0; i < array.length; i++) {
    if (array[i] === 'a') {
        a_counter++;
    }
    if (array[i] === 'b') {
        b_counter++;
    }
}
return (a_counter > 0 && b_counter > 0);

But this isn't very short. 但这不是很短。 I can do indexOf but that will loop through twice. 我可以做indexOf但这将循环两次。 I have also considered using a set as below: 我还考虑过使用如下设置:

const letter_set = new Set(array)
return (letter_set.has('a') && letter_set.has('b')) 

But I am pretty unfamiliar with sets and don't know if this solution could potentially be more expensive than just looping. 但是我对集合非常陌生,不知道这种解决方案是否可能比循环更昂贵。 I know that has() operations should be faster than array iterations but constructing the set probably takes at least O(N) time (I'm assuming). 我知道has()操作应该比数组迭代更快,但是构造集合可能至少需要O(N)时间(我假设)。

Is there a clean and efficient way to find multiple elements in an array? 是否有一种干净有效的方法来查找数组中的多个元素? ES6 answers welcome ES6解答欢迎

You could use just the Set and check if the wanted items are in the items array. 您可以仅使用Set并检查所需项目是否在items数组中。

 const check = (items, wanted) => wanted.every(Set.prototype.has, new Set(items)); console.log(check(['a', 'b', 'a'], ['a', 'b'])); // true console.log(check(['a', 'b', 'c'], ['a', 'b'])); // true console.log(check(['a', 'c', 'c'], ['a', 'b'])); // false 

You can use every and includes to do this check. 您可以使用everyinclude来执行此检查。

So we are saying every item must be included in the array. 所以我们说每个项目都必须包含在数组中。

 function contains(arr, ...items) { return items.every(i => arr.includes(i)) } console.log(contains(['a', 'b', 'a'], 'a', 'b')) console.log(contains(['a', 'c', 'c'], 'a', 'b')) console.log(contains(['a', 'b', 'c'], 'a', 'b', 'c')) console.log(contains(['a', 'b', 'c', 'd'], 'a', 'b', 'c', 'd', 'e')) 

array.includes('a') && array.includes('b')

即使有多个元素, includes似乎也是检查特定元素的一种便捷方法。

Not as compact as the other examples, but it does do the job in single run. 它不像其他示例那样紧凑,但是可以单次运行。

 const arr1 = ['a', 'b', 'a']; //method should return true const arr2 = ['a', 'c', 'c']; //method should return false const arr3 = ['a', 'b', 'c']; //method should return true const reducer = ({ a, b }, char) => ({ a: a || char === 'a', b: b || char === 'b' }); const includesAnB = arr => { const { a, b } = arr.reduce(reducer, {}); return a && b; } console.log(includesAnB(arr1)); console.log(includesAnB(arr2)); console.log(includesAnB(arr3)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM