返回阵列的函数,该数组是其他两个数组的交点

发布于 2025-02-06 12:33:13 字数 686 浏览 3 评论 0原文

function arraysCommon(array1, array2) {
  return array1.filter(x => array2.includes(x));
}

此功能无法按照我想要的方式工作。 例如给定array1 = [1,2,3,2,1]array2 = [5,4,3,2,1] 它返回[1,2,3,2,1],因为在两个数组中都可以看到elements 1,2,3。 但是我希望它以该顺序返回[1,2,3],因为1,2,3array2和被视为单独的实体。

因此,功能几乎应该是

  • 第一个数组中的每个元素都可以映射到第二个数组中的最多元素。
  • 每个数组中的重复元素被视为单独的实体。
  • 第一个数组决定了我试图循环循环阵列的顺序

,并检查和比较每个数组中的重复项数量,但我似乎无法使逻辑正常工作。有其他方法可以解决这个问题吗?

我附上了两个Venn图的图像,可以阐明差异 “我已经附加了两个Venn图的图像,这些图像可能会澄清差异”

function arraysCommon(array1, array2) {
  return array1.filter(x => array2.includes(x));
}

This function does not work the way I want it to.
For instance given array1 = [1,2,3,2,1] and array2 = [5,4,3,2,1]
it returns [1,2,3,2,1], since the elements 1,2,3 are seen in both arrays.
But I want it to return [1,2,3] in that order since 1,2,3 are seen only once in array2 and are treated as seperate entities.

So pretty much the functionality should be that

  • Each element in the first array can map to at most one element in the second array.
  • Duplicated elements in each array are treated as separate entities.
  • the first array determines the order

I have attempted to loop through the arrays and check and compare the number of duplicates in each array but I can't seem to get the logic working correctly. Is there a different way to approach this?

I've attached an image of two Venn diagrams that might clarify the difference
I've attached an image of two Venn diagrams that might clarify the difference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看春风乍起 2025-02-13 12:33:13

不幸的是,它变得更加复杂,因为您需要知道已经添加了哪些数字。在这种情况下,您需要一个临时数组来保留结果。我们还需要跟踪数组中是否存在两次数字。

尝试以下操作:

function arraysCommon(array1, array2) {
  //Copy array2 by duplicating and spreading the elements into another array.
  var copyArray2 = [...array2];
  //Temperary Array
  var temp = [];
  for (let x of array1) {
      //Check if the element is in the first array and not already added to the temp array
      if (copyArray2.includes(x)) {
          temp.push(x);
          //Remove item from copy array2 so it cannot be used anymore
          copyArray2.splice(copyArray2.indexOf(x), 1);
      }
  } 
  //Return the temp array
  return temp;
}

console.log(arraysCommon([1,2,3,2,1], [5,4,3,2,1]))
console.log(arraysCommon([1,2,3,2,1], [2,2,3,3,4]))

Unfortunately, it gets more complicated because you need to know what numbers you have already added. In this case you need a temporary array to hold the result. We also need to track if a number exists in the array two times.

Try this:

function arraysCommon(array1, array2) {
  //Copy array2 by duplicating and spreading the elements into another array.
  var copyArray2 = [...array2];
  //Temperary Array
  var temp = [];
  for (let x of array1) {
      //Check if the element is in the first array and not already added to the temp array
      if (copyArray2.includes(x)) {
          temp.push(x);
          //Remove item from copy array2 so it cannot be used anymore
          copyArray2.splice(copyArray2.indexOf(x), 1);
      }
  } 
  //Return the temp array
  return temp;
}

console.log(arraysCommon([1,2,3,2,1], [5,4,3,2,1]))
console.log(arraysCommon([1,2,3,2,1], [2,2,3,3,4]))

断念 2025-02-13 12:33:13

通过分类和计数应该是可能的。由于当您找到类似字符时,您正在增加,所以这应该没关系:

const array1= [1,4,1,1,5,9,2,7];
const array2 = [1,8,2,5,1]

const array3 = [1,2,3,2,1];
const array4 = [5,4,3,2,1,2]

const array5 = [1,2,3,2,1];
const array6 = [2,2,3,3,4]
function arraysCommon(array1, array2) {
  const ans = [];
  array1.sort();
  array2.sort();
  let j = 0;
  let i = 0;
  while(i<array1.length && j<array2.length){
    if(array1[i] === array2[j]){
      ans.push(array1[i]);
      i++;
      j++;
    }
    else if(array2[i] > array1[j]){
    i++;
    }
    else{
    j++;
    }
  }
  console.log(ans);
}

arraysCommon(array1,array2);
arraysCommon(array3,array4);
arraysCommon(array5,array6);

With sorting and counting this should be possible. Since you are incrementing when you find similar characters, this should be okay:

const array1= [1,4,1,1,5,9,2,7];
const array2 = [1,8,2,5,1]

const array3 = [1,2,3,2,1];
const array4 = [5,4,3,2,1,2]

const array5 = [1,2,3,2,1];
const array6 = [2,2,3,3,4]
function arraysCommon(array1, array2) {
  const ans = [];
  array1.sort();
  array2.sort();
  let j = 0;
  let i = 0;
  while(i<array1.length && j<array2.length){
    if(array1[i] === array2[j]){
      ans.push(array1[i]);
      i++;
      j++;
    }
    else if(array2[i] > array1[j]){
    i++;
    }
    else{
    j++;
    }
  }
  console.log(ans);
}

arraysCommon(array1,array2);
arraysCommon(array3,array4);
arraysCommon(array5,array6);

醉梦枕江山 2025-02-13 12:33:13

这应该按照您的意愿起作用!

// test 1 
const array1 = [1,4,1,1,5,9,2,7];
const array2 = [1,8,2,5,1];

// test 2
const array3 = [1,2,3,2,1];
const array4 = [5,4,3,2,1];

const mapper = (array1, array2) => {
  var obj = {};
  array1.forEach((x, indexX) => {
    array2.forEach((y, indexY) => {
      if (x == y) {
        if (!Object.values(obj).includes(indexX) && !obj.hasOwnProperty(indexY)) {
          obj[indexY] = indexX;
          return;
        }
      }
    }) 
  })
  return Object.values(obj).sort().map(values => array1[values]);
}

console.log(mapper(array1, array2));
console.log(mapper(array3, array4));

我希望这会有所帮助。干杯。

this should work as you wanted!

// test 1 
const array1 = [1,4,1,1,5,9,2,7];
const array2 = [1,8,2,5,1];

// test 2
const array3 = [1,2,3,2,1];
const array4 = [5,4,3,2,1];

const mapper = (array1, array2) => {
  var obj = {};
  array1.forEach((x, indexX) => {
    array2.forEach((y, indexY) => {
      if (x == y) {
        if (!Object.values(obj).includes(indexX) && !obj.hasOwnProperty(indexY)) {
          obj[indexY] = indexX;
          return;
        }
      }
    }) 
  })
  return Object.values(obj).sort().map(values => array1[values]);
}

console.log(mapper(array1, array2));
console.log(mapper(array3, array4));

I hope this helps. Cheers.

流年里的时光 2025-02-13 12:33:13

您可以实现一个新的集合,Wich仅带来唯一的值,而不是从该集合中重新保留一个数组。

这样的事情:

function arraysCommon(array1, array2) {
  const filtered = array1.filter(x => array2.includes(x));
  const uniqueValues = new Set(filtered)
  return Array.from(uniqueValues)
}

You can instance a new Set, wich brings only unique values and than retorn a array from this set.

Something like this:

function arraysCommon(array1, array2) {
  const filtered = array1.filter(x => array2.includes(x));
  const uniqueValues = new Set(filtered)
  return Array.from(uniqueValues)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文