在数组之间查找重复项

7
假设您有两个长度恒定为3的整数数组,并且您始终确信给定的两个数组中的两个元素将具有相同的值。
因此,假设数组A有三个值:a,b,c。 数组B有三个值:d,e,f。
我们确定其中两个值将是相同的。我们被要求将这四个不同的值放入大小为4的数组中,使得输出数组C在索引1和2处具有来自数组A和B的相同值。在索引0和3处,它应该具有数组A和B的不同值。我已经实现了它,但对这个解决方案真的不满意...有人有更好的解决方案吗?除了将我的计数器放入数组中的解决方案... :)
int[] a = { 1, 201, 354 };
int[] b = { 404, 201, 354 };

int[] c = new int[4];

for (int i = 0; i < c.Length; i++)
{
    Console.WriteLine(c[i]);
}

不是的,我正在寻找不规则网络的三角测量,对于每个点,如果它周围有两个以上的三角形,我想测量角度... - kl.
1
我太懒了,不想写一个带有join和count重复项的LINQ。 - Dead account
谢谢你诚实的Ian :)))) - kl.
2
@kl:你关于三角测量的评论增加了你的问题的可信度 - 为什么不编辑一下以提供上下文呢? - John K
16个回答

0

我想到了这个作为第一稿,但我认为它需要一些改进。它也没有满足在数组中将重复项放在位置1和2,唯一数字放在0和3的要求。不过我还是想发布它,这样你就可以了解它的外观:

  int[] a = { 1, 201, 354 };
  int[] b = { 404, 201, 354 };

  int[] c = new int[ 4 ];

  // Start by just copying over one of the arrays completely.
  a.CopyTo( c, 0 );

  // Loop through b and compare each number against each
  // each number in a.
  foreach( int i in b )
  {
    // Assume that you're not dealing with a duplicate
    bool found = true;
    foreach( int j in a )
    {
      // If you find a duplicate, set found to false
      if( i == j )
      {
        found = false;
      }           
    }
    // If you haven't found a duplicate this is the
    // number you want - add it to the array.
    if (found == true)
    {
      c[3] = i;
    }
  }

0

我试图给出一个简短的答案。但是它假设输入是正确的。

int c1, c2, i;
c1 = a[0] == b[0] ? 0
                  : (a[0] == b[1] ? 1 : 2); // index of a[0] in array 'b'
c2 = a[1] == b[0] ? 0
                  : (a[1] == b[1] ? 1 : 2); // index of a[1] in array 'b'

for(i=0; i<2; i++)
    Console.WriteLine(a[i]);
Console.WriteLine(b[3-c1-c2]); // looks quite hacky but it is actually element of 'b' not in array 'a'

0
这里有一些简单的代码,但它假设a和b的值始终是正数。
int[] a = { 1, 201, 354 };
int[] b = { 404, 201, 354 };

int[] c = { -1, -1, -1, -1};

for(int i = 0; i < 3; i++){
    int notfound = 1;
    for(int j = 0; j < 3; j++){
        if(b[j] == -1) continue;

        if(a[i] == b[j]){
            b[j] = -1;
            if(c[1] == -1)
                c[1] = a[i];
            else
                c[2] = a[i];
            notfound = 0;
            break;
        }
    }
    if(notfound)
        c[0] = a[i];
}
int k = 0;
while(b[k++] == -1);
c[3] = b[k];

我没有测试过,但希望你能理解这个想法。这个方法使用非常少的额外空间(只需要为notfound分配空间,它可以被设置为布尔值,以及索引变量),并且应该非常快速。


0
int[] a = { 204, 534, 1 };
int[] b = { 204, 534, 401 };
int[] c = new int[4];

int x = 3, y = 3, k = 1;
for(int i=0; i<3; i++)
    for(int j=0; j<3; j++)
        if (a[i] == b[j]) {
            c[k++] = a[i];
            x -= i;
            y -= j;
            break;
        }
c[0] = a[x];
c[3] = b[y];

0
Sapph提供的答案非常简洁明了,但如果性能非常重要,这里有一个更好的选择。.NET数组边界检查可能会增加一些开销,但在C语言中,这将编译为64条指令而没有分支。
int[] a = { 204, 534, 1 };
int[] b = { 204, 534, 401 };
int[] c = new int[4];

// pick the value from a that is not in b for c[0]
// a[0] not in b is implied by a[1] in b and a[2] in b
int a1_not_in_b = Convert.ToInt32(a[1] != b[0] & a[1] != b[1] & a[1] != b[2]);
int a2_not_in_b = Convert.ToInt32(a[2] != b[0] & a[2] != b[1] & a[2] != b[2]);

// bitfield of 2 bit values equivalent to the array {0,1,2,0,1}
int idxs = 0 | 1 << 2 | 2 << 4 | 0 << 6 | 1 << 8;
// if a[1] not in b start at 1, if a[2] not in b start at 2, else start at 0
idxs >>= 2*a1_not_in_b | 4*a2_not_in_b;
c[0] = a[(idxs >> 0) & 3];
c[1] = a[(idxs >> 2) & 3];
c[2] = a[(idxs >> 4) & 3];

// pick the value from b that is not in a
// b[0] not in a is implied by b[1] in a and b[2] in a
int b1_not_in_a = Convert.ToInt32(a[0] != b[1] & a[1] != b[1] & a[2] != b[1]);
int b2_not_in_a = Convert.ToInt32(a[0] != b[2] & a[1] != b[2] & a[2] != b[2]);
c[3] = b[b1_not_in_a | 2*b2_not_in_a];

0

这个怎么样?

private static int[] FindDuplicates(int[] arrA,int[] arrB)
    {
        var aList=new List<int>();
        Array.Sort(arrA);
        Array.Sort(arrB);


        for(int i=0;i<arrA.Length;i++)
        {
           if(arrB.Contains(arrA[i]))
           {           
           aList.Add(arrA[i]);
           }

        }
        return aList.ToArray();

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接