检测交错数组中的类似元素

本文关键字:元素 数组 检测 | 更新日期: 2023-09-27 18:11:24

有没有办法检测多维数组中的类似元素?例如:

int[][] arrayA = {{1 , 2}, {4 , 6}, {3, 7}};
int[][] arrayB = {{3 , 2}, {1 , 2}, {8, 5}};

arrayAarrayB 都具有元素 {1 , 2} 。(或者简单地说,任何共同的元素(有没有办法检测它是true

检测交错数组中的类似元素

是的,您只需要对逻辑进行编码即可进行检测。

    bool result = false;
    foreach (var arrayAItem in arrayA)
    {
        foreach (var arrayBItem in arrayB)
        {
            if (arrayAItem.SequenceEqual(arrayBItem))
            {
                result = true;
                break;
            }
        }
        if (result == true)
        {
            break;
        }
    }

和单衬

bool result = arrayA.Any(arrayAItem => arrayB.Any(arrayBItem => arrayAItem.SequenceEqual(arrayBItem)));

如果不给你代码,很难给你答案,但是:

对于数组 A 中的每个元素,检查它是否等于数组 B 中的每个元素。所以基本上,你需要一个嵌套的foreach。如果要记录它们具有哪些共同的元素,请将它们添加到List<ElementType>(而不是数组,因为您要动态填充它(。

这不是那么简单,因为您的数组是嵌套的。你不能只检查arrayA[x] == arrayB[x]因为你要比较数组,这些数组是引用类型(除非它们都指向同一块内存,否则它会返回 false(。相反,您必须在两个foreach内再添加一个循环,您已经必须检查arrayA中的每个intarrayB中的相关循环。 int是一种值类型,因此==比较的行为方式符合您的预期。

以下是您执行此操作的方法(不仅如此(:

var commonItems = from innerA in arrayA
                  from innerB in arrayB
                  where innerA.SequenceEqual(innerB)
                  select innerA;
bool anyCommon = commonItems.Any();
var firstCommon = commonItems.FirstOrDefault();
var commonCount = commonItems.Count();

我想变量名称是不言自明的:-(

有这样的东西吗?

arrayA.Where(arr => arrayB.Any(arr2 => arr1.OrderBy(x => x)
                                           .SequenceEquals(arr.OrderBy(x => x)))).ToArray()

如果需要,您可以删除两个OrderBy调用,具体取决于您希望如何将两个数组视为"相同"。

以下代码会变长,但会提供所需的输出:

    private void TestArrays()
    {
        int[,] arrayA = new[,] { { 1, 2 }, { 4, 6 }, { 3, 7 } };
        int[,] arrayB = new[,] { { 3, 2 }, { 1, 2 }, { 8, 5 } };
        int parentLengthA = arrayA.GetLength(0);
        int childLengthA = arrayA.GetLength(1);
        int parentLengthB = arrayB.GetLength(0);
        int childLengthB = arrayB.GetLength(1);
        int[] itemsOfA;
        int[] itemsOfB;
        List<int[]> matchedArrays = new List<int[]>();
        for (int i = 0; i < parentLengthA; i++)
        {
            itemsOfA = new int[childLengthA];
            for (int j = 0; j < parentLengthB; j++)
            {
                itemsOfB = new int[childLengthB];
                bool isMatched = true;
                if (itemsOfA.Length != itemsOfB.Length)
                {
                    isMatched = false;
                    break;
                }
                for (int k = 0; k < itemsOfA.Length; k++)
                {
                    if (arrayA[i, k] != arrayB[j, k])
                    {
                        isMatched = false;
                        break;
                    }
                    else
                    {
                        itemsOfA[k] = arrayA[i, k];
                    }
                }
                if (isMatched)
                {
                    matchedArrays.Add(itemsOfA);
                }
            }
        }
        //Just to output the matched array(s)
        if (matchedArrays.Count > 0)
        {
            StringBuilder sb = new StringBuilder();
            foreach (int[] matchedArray in matchedArrays)
            {
                foreach (int i in matchedArray)
                {
                    sb.Append(i + ",");
                }
                sb.AppendLine();
            }
            MessageBox.Show(sb.ToString());
        }
    }