You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检查数组是否包含另一数组的元素?以A=[0,1,2]、B=[2,1,0]为例

Hey there! Let's tackle your two array-related questions step by step—these are super common tasks, so I'll walk you through practical, actionable solutions.

1. General Scenario: How to Check if One Array Contains All Elements of Another Array

There are a few solid approaches here, depending on your priorities like performance, code simplicity, or browser compatibility:

Approach 1: Use Set for Optimized Lookup Performance

Converting the target array to a Set makes element lookups O(1) instead of O(n), which is way more efficient—especially with large datasets:

function doesArrayContainAll(sourceArr, targetArr) {
  const targetSet = new Set(targetArr);
  return sourceArr.every(element => targetSet.has(element));
}

Why this works: Set.has() is far faster than Array.includes() for repeated checks. The overall time complexity here is O(n + m) (n = length of source array, m = length of target array), which is a huge upgrade over the O(n*m) complexity of the next method.

Approach 2: every() + includes() (Most Concise Code)

If you're working with small arrays and prioritize readability over raw performance, this one-liner style is perfect:

function doesArrayContainAll(sourceArr, targetArr) {
  return sourceArr.every(item => targetArr.includes(item));
}

Note: This method loops through the target array every time it checks an element from the source, so it can get slow with large arrays.

Approach 3: Manual Nested Loops (Best Compatibility)

If you need to support older environments that don't have ES6+ features, a manual loop is reliable:

function doesArrayContainAll(sourceArr, targetArr) {
  for (let i = 0; i < sourceArr.length; i++) {
    let elementFound = false;
    for (let j = 0; j < targetArr.length; j++) {
      if (sourceArr[i] === targetArr[j]) {
        elementFound = true;
        break;
      }
    }
    if (!elementFound) return false;
  }
  return true;
}
2. Verifying if Elements of A = [0,1,2] Exist in B = [2,1,0]

Since B is just a reversed version of A (all elements are identical, just ordered differently), any of the methods above will work. Here are concrete examples tailored to your specific arrays:

Using the Set Method

const A = [0, 1, 2];
const B = [2, 1, 0];

const bElementSet = new Set(B);
const allAInB = A.every(num => bElementSet.has(num));
console.log(allAInB); // Output: true

Using the Concise every() + includes()

const A = [0, 1, 2];
const B = [2, 1, 0];

const allAInB = A.every(num => B.includes(num));
console.log(allAInB); // Output: true

Bonus: Check if Both Arrays Have Exact Same Elements (Order Doesn't Matter)
If you want to confirm that B also contains no extra elements beyond A, you can sort both arrays and compare:

const haveIdenticalElements = (arr1, arr2) => {
  if (arr1.length !== arr2.length) return false;
  const sortedArr1 = [...arr1].sort();
  const sortedArr2 = [...arr2].sort();
  return sortedArr1.every((val, index) => val === sortedArr2[index]);
};

console.log(haveIdenticalElements(A, B)); // Output: true

This ensures a two-way match—great if you need to confirm the arrays are "equivalent" regardless of order.

内容的提问来源于stack exchange,提问作者Vikram S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:01:40