查找阵列之间重复

查找阵列之间重复

本文介绍了查找阵列之间重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你正在给定长度为3的整数两个数组,而你总是确保提供两个arrray的两个元素都会有相同的价值观。

所以假设数组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 =新INT [4];

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

解决方案

我很抱歉,我重读更密切,我想这是你想要的。请指教。 :)

  INT []相同= a.Intersect(B).ToArray(); ;
INT []的diff = a.Union(二)。除(相同).ToArray();
INT [] C =新INT [] {的diff [0],同[0],同[1],的diff [1]};
 

Assume you are given two arrays of integers of constant length which is 3, and you are always sure that two elements of the given two arrray will have same values.

so assume array A has three values: a, b, c.and array B has three values: d, e, f.

we are sure that two of the values will be same. we are asked to put these four different values in an array of size 4, such that output array C, should have in indices 1 and 2 the same values from arrays A and B. and at indices 0 and 3 it should have the different values of array A and B. i have implemented it, but really not satisfied with this solution... does anyone has better solution idea? except the one that would put my counters in array... :)

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]);
}
解决方案

I'm sorry, I re-read more closely and I think this is what you want. Please advise. :)

int[] same = a.Intersect(b).ToArray(); ;
int[] diff = a.Union(b).Except(same).ToArray();
int[] c = new int[] { diff[0], same[0], same[1], diff[1] };

这篇关于查找阵列之间重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:06