bool hasDuplicate = false;
int[] a = new int[] {1, 2, 3, 4};
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
我需要将数组A的所有元素与数组B的元素进行比较,并在B中出现重复元素的情况下,将hasDuplicate设置为TRUE。
bool hasDuplicate = false;
int[] a = new int[] {1, 2, 3, 4};
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
hasDuplicates = a.Intersect(b).Any();
您可以使用LINQ的Intersect
方法 - http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect.aspx
我知道您不想要单行解决方案,但我将为其他可能需要同一问题的用户留下我的答案。
如果您不想使用 Linq
,您可以使用 SequenceEqual。
bool equal = Array1.SequenceEqual(Array2);
希望这有所帮助。
既然这是一道作业题,那么我会给你一个作业答案。
当然,你可以使用LINQ并依赖于SequenceEqual
,Intersect
等方法,但那可能不是本练习的重点。
对于两个数组,你可以使用foreach
来遍历数组元素。
int[] someArray;
foreach(int number in someArray)
{
//number is the current item in the loop
}
如果你有两个相对较小的数组,可以循环遍历第一个数组中的每个数字,然后循环遍历第二个数组中的所有项目并进行比较。让我们尝试一下。首先,我们需要纠正你的数组语法。它应该看起来像这样:
int[] a = new int[] {1, 2, 3, 4};
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
{
的方法。您使用这种语法来创建一个N维数组。bool hasDuplicate = false;
int[] a = new int[] { 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 7, 8 };
foreach (var numberA in a)
{
foreach (var numberB in b)
{
//Something goes here
}
}
hasDuplicate
设置为 true。bool hasDuplicate = false;
int[] a = new int[] { 8, 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 7, 8 };
foreach (var numberA in a)
{
foreach (var numberB in b)
{
if (numberA == numberB)
{
hasDuplicate = true;
}
}
}
这是一种非常“暴力”的方法。循环的复杂度为O(n2),但在您的情况下可能并不重要。使用LINQ的其他答案肯定更有效率,如果效率很重要,您可以考虑使用这些答案。另一种选择是使用break
停止循环,如果hasDuplicate
为true,或者将此代码放入一个方法中并使用return
退出该方法。
不是最高效的方法,但可能是最易于理解的方法,类似于这样:
foreach (int _a in a) { // iterate through all elements in array a (as _a)
foreach (int _b in b) { // iterate through all elements in array b (as _b)
if (_a == _b) { // if we've got a duplicate
hasDuplicates = true; // store that for later on
break; // immediately leave this loop (no point in further looking up)
}
}
if (hasDuplicates) { // if we've got a duplicate
break; // leave this loop as well (no point in further looking up)
}
}
如果您想学习并想要编写算法,那么使用LINQ和其他技术是不会帮助到您的。
您需要嵌套2个foreach
(或for
,以您喜欢的为准)循环,一旦在第一个循环中找到与第二个循环中的成员匹配的成员,则将布尔变量设置为true并break
循环。
LINQ
可以用一行代码完成此操作,但最好了解其工作原理,因为您在问题中提到了算法这个词:)public bool IsPresentInArray(int[] firstArray, int[] secondArray)
{
foreach (var itemA in firstArray)
{
foreach (var itemB in secondArray)
{
if (itemB == itemA)
{
return true;
}
}
}
return false;
}
int[] a = new int[]{1, 2, 3, 4};
int[] b = new int[] { 5, 6, 1, 2, 7, 8};
bool present= IsPresentInArray(a, b);
了解foreach循环,请点击这里.
HashSet
。此外,您可以在找到第一个匹配项后立即退出循环:HashSet<int> h = new HashSet<int>(a);
foreach (int i in b) {
if (h.Contains(i)) {
hasDuplicate = true;
break;
}
}
相比于使用两个嵌套循环比较所有值的O(n*m)解决方案,这是一个O(n+m)的解决方案。
为什么不尝试使用LINQ呢?看一下以下代码:
public bool Checking()
{
bool hasDuplicate = false;
int[] a = new int[] { 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
int count = a.Intersect(b).Count();
if (count >= 1)
hasDuplicate = true;
return hasDuplicate;
}
我用 for
循环完成了。关键是我们将每个成员与数组 b
中的成员进行比较。所以首先将 a[0]
与数组 b
的每个成员进行比较,然后再进行 a[1]
,依此类推,直到找到匹配项。
bool hasDuplicate = false;
int[] a = new int[] { 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < b.Length; j++)
{
if (a[i] == b[j])
{
hasDuplicate = true;
}
}
}