比较C#中的两个数组
bool hasDuplicate = false; int[] a = new int[] {1, 2, 3, 4}; int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
我需要将数组A的所有元素与数组B的元素进行比较,如果B中有重复元素,则在TRUE上设置hasDuplicate。
由于这是家庭作业,我会给你一个作业答案。
当然,你可以使用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(n 2 ),但在您的情况下这可能无关紧要。 使用LINQ的其他答案肯定更有效,如果效率很重要,你可以考虑那些。 另一个选择是如果hasDuplicate
为true,则使用break
来“停止”循环,或者将此代码放在方法中并使用return
退出方法。
hasDuplicates = a.Intersect(b).Any();
您可以使用LINQ Intersect
方法 – http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect.aspx
如果学习是你所寻求的,而算法是你想要的,那么使用LINQ和任何其他爵士乐都无济于事。
你需要有2个嵌套的foreach
(或者你喜欢的任何一个)循环,一旦你在第一个循环中找到一个成员匹配第二个循环中的成员,就将你的boolean变量设置为true并break
循环
不是最高效,但可能最容易理解的方法是这样的:
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) } }
显然,这不是最高性能的解决方案,因为复杂性为O(n²)
,这意味着任何一个数组中元素数量的两倍将使完成操作所需的时间加倍(最坏的情况); 两个数组中元素数量的两倍将使时间量翻两番。
更优雅的解决方案是使用一些其他解决方案中描述的预定义方法,但由于这是家庭作业的东西,我不希望你被允许使用这些“快捷方式”(或者应该这样做)。
永远记住:即使您在这里找到解决方案,也要尝试理解它们,将它们用作灵感,然后编写自己的解决方案。 这可能是最好的学习方式。 不要只是复制和粘贴。
尽管LINQ
将帮助您使用一行代码执行此操作,但最好了解它是如何工作的,因为您在问题中提到了算法这个词:)
循环遍历数组并将每个项目与第二个数组中的项目进行比较。 如果存在,则返回true。 别的假。 我会把它包装在这样的函数中
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循环
我知道你不想要一个单行的解决方案,但我会给其他用户留下我的答案,他们可能想要一个简单的解决方案来解决同样的问题。
如果您不想使用Linq
,可以使用SequenceEqual 。
bool equal = Array1.SequenceEqual(Array2);
希望能帮助到你。
要有效地将一个集合中的所有元素与另一个集合进行比较,您可以创建其中一个元素的HashSet
。 此外,您可以在找到第一个匹配后立即退出循环:
HashSet h = new HashSet (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; }
我使用“IndexOf”和“foreach”循环来创建它。 (注意:前3个“字符串”行只是一个如何创建数组并使其成为正确格式的示例)。
如果要比较2个数组,它们将以分号分隔,但最后一个值后面没有一个数组。 如果在数组的字符串forms中附加一个分号(即a; b; c变为a; b; c;),则可以使用“x;”匹配 无论它处于什么位置:
bool found = false; string someString = "abc"; string[] arrString = someString.Split('-'); string myStringArray = arrString.ToString() + ";"; foreach (string s in otherArray) { if (myStringArray.IndexOf(s + ";") != -1) { found = true; break; } } if (found == true) { // .... }
我用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; } } }