2012-06-15 104 views
0
bool hasDuplicate = false; 
int[] a = new int[] {1, 2, 3, 4}; 
int[] b = new int[] { 5, 6, 1, 2, 7, 8 }; 

我需要数组A中的所有元素与阵列B的元素,并在乙重复元件,设置hasDuplicate上TRUE的情况下进行比较。在C#比较两个阵列

+0

如果您发布了您尝试过的内容,我们可能会指出您错误的地方,也许。既然你没有,[我们不必为你做功课](http://meta.stackexchange.com/a/128572)。 –

+0

严肃地说,该代码不会编译。看看你的数组声明 –

+0

现在客户声明应该没问题 – GibboK

回答

4

由于这是家庭作业,我会给你一个功课回答。

当然,你可以使用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 ),但在您的情况下可能无关紧要。使用LINQ的其他答案肯定更有效,如果效率很重要,那么可以考虑这些。另一种选择是使用break“停止”循环,如果hasDuplicate为真,或将此代码放在方法中并使用return退出该方法。

3

我知道你不想要一个在线解决方案,但我会离开我为其他用户回答谁可能要对同一问题的简单解决方案。

如果你不想使用Linq,你可以使用SequenceEqual

bool equal = Array1.SequenceEqual(Array2);

希望它能帮助。

4

如果学习是你寻求和算法中是你想要什么样拿出,然后使用LINQ和其他爵士不会帮你。

你需要有2套foreach(或for,无论你喜欢)循环,一旦你在第一圈的成员在第二循环中找到的匹配一员,您的布尔变量设置为true,break

+0

+1真是这样做的算法。 – phadaphunk

4

不是最高效的,但可能是最容易理解的方法是这样的:

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²),这意味着元素数量的两倍在任何一个阵列将增加一倍所需的时间来完成操作(最坏情况)的量;在两个阵列元件的数量的两倍将四倍的时间量。

更优雅的解决方案是使用其他一些解决方案中描述的预定义的方法,但由于这是作业的东西,我不认为您可以使用这些“快捷方式”(或应该做的所以)。

一定要记住:即使你在这里找到解决方案,试着去了解他们,使用他们的灵感,然后自己编写。这可能是最好的学习方式。不要只是复制粘贴&。

3

Eventhough 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循环here

+0

非常感谢这个 – GibboK

+0

@GibboK:你好,欢迎光临。 :) 很高兴我能帮上忙。 – Shyju

2

要有效比较一组中的所有元素到另一组中,您可以制作其中一个组的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)解决方案。

0

我用一个“IndexOf”和“foreach”循环来创建这个。 (注意:前3个“字符串”行只是您如何创建数组并将其转换为正确格式的示例)。

如果要比较2个数组,它们将用分号分隔,但最后一个值在后面不会有一个。如果在数组的字符串形式(即a; b; c变成a; b; c;)后附加一个分号,可以使用“x;”匹配不管它在什么位置:

bool found = false; 
string someString = "a-b-c"; 
string[] arrString = someString.Split('-'); 
string myStringArray = arrString.ToString() + ";"; 

foreach (string s in otherArray) 
{ 
    if (myStringArray.IndexOf(s + ";") != -1) { 
     found = true; 
     break; 
    } 
} 

if (found == true) { 
    // .... 
} 
0

为什么我们不尝试使用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; 

    } 
0

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; 
     } 
    } 
}