int[] a = { 1, 2, 3, 4, 5, 9 }; int[] b = { 1, 4, 5, 7, 8, 9 }; string s_a = ""; string s_b = ""; foreach (int i in a) { s_a += i.ToString() + ','; } foreach (int i in b) { s_b += i.ToString() + ','; } string value = "不相同的元素:"; foreach (int i in a) { if (!s_b.Contains(i.ToString())) value += i.ToString() + ","; } foreach (int i in b) { if (!s_a.Contains(i.ToString())) value += i.ToString() + ","; } Console.WriteLine(value); /* ------输出结果------------ 不相同的元素:2,3,7,8, */
bool IsEquals(int[]a,int[]b) { for(int i=0;i <a.Length;i++) if(a[i]!=b[i]) lst.Add(a[i]); } 或List<int> list = new List<int>(a); List<int> list2 = new List<int>(b); foreach(int i in list) { if(!list2.Contains(i)) {} }
bool IsEquals(int[]a,int[]b) { for(int i=0;i <a.Length;i++) if(a[i]!=b[i]) lst.Add(a[i]); } 或 List <int> list = new List <int>(a); List <int> list2 = new List <int>(b); foreach(int i in list) { if(!list2.Contains(i)) {} } 楼上的,最简单的算法
似乎有点欠妥? 没有考虑到第二个数组拥有二第一个数组没有的? static void Main(string[] args) { int[] a = { 1, 2, 3, 4, 5, 9 }; int[] b = { 1, 4, 5, 7, 8, 9 }; string result = "没有的元素:"; foreach (int item in a) { if (Array.IndexOf<int>(b, item) == -1) { result = string.Concat(result, item, ","); } } foreach (int item in b) { if (Array.IndexOf<int>(a, item) == -1) { result = string.Concat(result, item, ","); } } Console.WriteLine(result); Console.ReadKey(); }
private void button1_Click(object sender, EventArgs e) { int[] a = { 1, 2, 3, 4, 5, 9 }; int[] b = { 1, 4, 5, 7, 8, 9 }; List<int> result = new List<int>(); foreach (int x in a)//找出数组a中的不同值 { int m = 0; foreach (int y in b) { if (x != y) m++; } if (m == b.Length)//m==a.Length说明都不相等 result.Add(x);//保存这个都不相等的值 } foreach (int x in b)//找出数组b中的不同值 { int m = 0; foreach (int y in a) { if (x != y) m++; } if (m == a.Length) result.Add(x); } foreach (int x in result) textBox1.AppendText(x + "\r\n"); }结果为: 2 3 7 8 以上方法适应两个任意长度的数组
不懂算法,用了比较笨的方法int[] a = { 1, 2, 3, 4, 5, 9 }; int[] b = { 1, 4, 5, 7, 8, 9 }; List<int> la = new List<int>(a); List<int> lb = new List<int>(b); List<int> lc = new List<int>(); foreach (int i in la) { if (!lb.Contains(i)) { lc.Add(i); } } foreach (int i in lb) { if (!la.Contains(i)) { lc.Add(i); } } //输出测试 foreach (int i in lc) { Console.WriteLine(i); } Console.ReadLine();
用归并写了一个。using System;namespace ConsoleApplication4 { class Program { static void Main(string[] args) { int[] a = { 1, 2, 3, 4, 5, 9, 10 }; int[] b = { 1, 4, 5, 7, 8, 9 }; Array.Sort(a); Array.Sort(b); int[] temp = null; int i = 0, j = 0, k = 0; while (i < a.Length && j < b.Length) { if (a[i] == b[j]) { i++; j++; } else if (a[i] > b[j]) { Console.WriteLine(b[j]); j++; } else { Console.WriteLine(a[i]); i++; } } if (i < a.Length) { temp = a; k = i; } else if (j < b.Length) { temp = b; k = j; } else return; for (int m = k; m < temp.Length; m++) Console.WriteLine(temp[m]); } } }
int[] A = { 1, 2, 3, 4, 5, 9 }; int[] B = { 1, 4, 5, 7, 8, 9 }; // int[] C = A.Intersect(B).ToArray(); // int[] D = A.Union(B).ToArray(); int[] F = A.Except(B).ToArray(); int[] E = B.Except(A).ToArray();
for (int i = 0; i < F.Length; i++) { Console.WriteLine(F[i].ToString()); } for (int j = 0; j < E.Length; j++) { Console.WriteLine(E[j].ToString());
}
不知道楼主是不是用的VS2008,我这里提供一个使用LINQ的高效算法:using System.Linq; int[] a = { 1, 2, 3, 4, 5, 9 }; int[] b = { 1, 4, 5, 7, 8, 9 }; var c = a.Cast<int>().Concat(b.Cast<int>()).GroupBy((t) => t).Where((t) => t.Count() == 1); foreach (var d in c) { MessageBox.Show(d.Key.ToString()); }
用linq查询,这样非常的方便。 class Program { static void Main(string[] args) { int[] i = new int[] { 1, 3, 5, 6 }; int[] j = new int[] { 3, 4, 5, 7 }; var linq = from var1 in i from var2 in j where var1 != var2 select var1; foreach (var a in linq) { Console.WriteLine(a.ToString());
} } }
写了个最蠢的方法... public class aaa { public static String check() { int a[] = new int[] { 1, 2, 3, 4, 5, 9 }; int b[] = new int[] { 1, 4, 5, 7, 8, 9 }; String c = ""; boolean d = false; int i = 0; // A数组和B数组比较找出A里面没有的值 for (i = 0; i < a.length; i++) { for (int j = 0; j < b.length; j++) { if (a[i] == b[j]) { d = false; break; } else d = true; } if (d == true) c += a[i]; } // B数组和A数组比较找出B里面没有的值 for (i = 0; i < b.length; i++) { for (int j = 0; j < a.length; j++) { if (b[i] == a[j]) { d = false; break; } else d = true; } if (d == true) c += b[i]; } return c; } public static void main(String[] agrs) { System.out.println(check()); }}
public class aaa { public static String check() { int a[] = new int[] { 1, 2, 3, 4, 5, 9 }; int b[] = new int[] { 1, 4, 5, 7, 8, 9 }; String c = ""; boolean d = false; int i = 0; // A数组和B数组比较找出A里面没有的值 for (i = 0; i < a.length; i++) { for (int j = 0; j < b.length; j++) { if (a[i] == b[j]) { d = false; break; } else d = true; } if (d == true) c += a[i]; } // B数组和A数组比较找出B里面没有的值 for (i = 0; i < b.length; i++) { for (int j = 0; j < a.length; j++) { if (b[i] == a[j]) { d = false; break; } else d = true; } if (d == true) c += b[i]; } return c; } public static void main(String[] agrs) { System.out.println(check()); }}
int[] b = { 1, 4, 5, 7, 8, 9 };
string s_a = "";
string s_b = "";
foreach (int i in a)
{
s_a += i.ToString() + ',';
}
foreach (int i in b)
{
s_b += i.ToString() + ',';
} string value = "不相同的元素:";
foreach (int i in a)
{
if (!s_b.Contains(i.ToString()))
value += i.ToString() + ",";
}
foreach (int i in b)
{
if (!s_a.Contains(i.ToString()))
value += i.ToString() + ",";
} Console.WriteLine(value);
/*
------输出结果------------
不相同的元素:2,3,7,8,
*/
{
for(int i=0;i <a.Length;i++)
if(a[i]!=b[i])
lst.Add(a[i]);
}
或List<int> list = new List<int>(a);
List<int> list2 = new List<int>(b);
foreach(int i in list)
{
if(!list2.Contains(i))
{}
}
bool IsEquals(int[]a,int[]b)
{
for(int i=0;i <a.Length;i++)
if(a[i]!=b[i])
lst.Add(a[i]);
}
或 List <int> list = new List <int>(a);
List <int> list2 = new List <int>(b);
foreach(int i in list)
{
if(!list2.Contains(i))
{}
}
楼上的,最简单的算法
似乎有点欠妥?
没有考虑到第二个数组拥有二第一个数组没有的?
static void Main(string[] args)
{
int[] a = { 1, 2, 3, 4, 5, 9 };
int[] b = { 1, 4, 5, 7, 8, 9 };
string result = "没有的元素:";
foreach (int item in a)
{
if (Array.IndexOf<int>(b, item) == -1)
{
result = string.Concat(result, item, ",");
}
} foreach (int item in b)
{
if (Array.IndexOf<int>(a, item) == -1)
{
result = string.Concat(result, item, ",");
}
}
Console.WriteLine(result);
Console.ReadKey();
}
{
int[] a = { 1, 2, 3, 4, 5, 9 };
int[] b = { 1, 4, 5, 7, 8, 9 };
List<int> result = new List<int>();
foreach (int x in a)//找出数组a中的不同值
{
int m = 0;
foreach (int y in b)
{
if (x != y)
m++;
}
if (m == b.Length)//m==a.Length说明都不相等
result.Add(x);//保存这个都不相等的值
} foreach (int x in b)//找出数组b中的不同值
{
int m = 0;
foreach (int y in a)
{
if (x != y)
m++;
}
if (m == a.Length)
result.Add(x);
} foreach (int x in result)
textBox1.AppendText(x + "\r\n");
}结果为:
2
3
7
8
以上方法适应两个任意长度的数组
int[] b = { 1, 4, 5, 7, 8, 9 };
List<int> la = new List<int>(a);
List<int> lb = new List<int>(b);
List<int> lc = new List<int>();
foreach (int i in la)
{
if (!lb.Contains(i))
{
lc.Add(i);
}
}
foreach (int i in lb)
{
if (!la.Contains(i))
{
lc.Add(i);
}
}
//输出测试
foreach (int i in lc)
{
Console.WriteLine(i);
}
Console.ReadLine();
{
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 2, 3, 4, 5, 9, 10 };
int[] b = { 1, 4, 5, 7, 8, 9 }; Array.Sort(a);
Array.Sort(b);
int[] temp = null; int i = 0, j = 0, k = 0; while (i < a.Length && j < b.Length)
{
if (a[i] == b[j]) { i++; j++; }
else if (a[i] > b[j]) { Console.WriteLine(b[j]); j++; }
else { Console.WriteLine(a[i]); i++; }
} if (i < a.Length) { temp = a; k = i; }
else if (j < b.Length) { temp = b; k = j; }
else return; for (int m = k; m < temp.Length; m++)
Console.WriteLine(temp[m]);
}
}
}
int[] A = { 1, 2, 3, 4, 5, 9 };
int[] B = { 1, 4, 5, 7, 8, 9 };
// int[] C = A.Intersect(B).ToArray();
// int[] D = A.Union(B).ToArray();
int[] F = A.Except(B).ToArray();
int[] E = B.Except(A).ToArray();
for (int i = 0; i < F.Length; i++)
{
Console.WriteLine(F[i].ToString());
}
for (int j = 0; j < E.Length; j++)
{
Console.WriteLine(E[j].ToString());
}
int[] b = { 1, 4, 5, 7, 8, 9 };
var c = a.Cast<int>().Concat(b.Cast<int>()).GroupBy((t) => t).Where((t) => t.Count() == 1);
foreach (var d in c)
{
MessageBox.Show(d.Key.ToString());
}
不知道LINQ的时间复杂度是多少,除开LINQ,归并排序应该是最高的了,T(n)=O(nlogn),其它几个都是O(n^2)。当然,就这么几个数讨论时间复杂度似乎有点多余。
{
static void Main(string[] args)
{
int[] i = new int[] { 1, 3, 5, 6 };
int[] j = new int[] { 3, 4, 5, 7 };
var linq = from var1 in i from var2 in j where var1 != var2 select var1;
foreach (var a in linq)
{ Console.WriteLine(a.ToString());
} }
}
public class aaa { public static String check() {
int a[] = new int[] { 1, 2, 3, 4, 5, 9 };
int b[] = new int[] { 1, 4, 5, 7, 8, 9 };
String c = "";
boolean d = false;
int i = 0; // A数组和B数组比较找出A里面没有的值
for (i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
if (a[i] == b[j]) {
d = false;
break;
} else
d = true;
}
if (d == true)
c += a[i];
}
// B数组和A数组比较找出B里面没有的值
for (i = 0; i < b.length; i++) {
for (int j = 0; j < a.length; j++) {
if (b[i] == a[j]) {
d = false;
break;
} else
d = true;
}
if (d == true)
c += b[i];
}
return c;
} public static void main(String[] agrs) {
System.out.println(check());
}}
public class aaa { public static String check() {
int a[] = new int[] { 1, 2, 3, 4, 5, 9 };
int b[] = new int[] { 1, 4, 5, 7, 8, 9 };
String c = "";
boolean d = false;
int i = 0; // A数组和B数组比较找出A里面没有的值
for (i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
if (a[i] == b[j]) {
d = false;
break;
} else
d = true;
}
if (d == true)
c += a[i];
}
// B数组和A数组比较找出B里面没有的值
for (i = 0; i < b.length; i++) {
for (int j = 0; j < a.length; j++) {
if (b[i] == a[j]) {
d = false;
break;
} else
d = true;
}
if (d == true)
c += b[i];
}
return c;
} public static void main(String[] agrs) {
System.out.println(check());
}}
arr1{1,2,3,4,5,9};
arr2{1,4,5,7,8,9};
arr3=arr2+arr1;
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr2.length;j++){
if(arr2[i]==arr1[i]){
arr3.remove(arr1[i],arr2[i]);
}
}
}
最后arr3 就是了
如果考虑性能的话就像楼上说的使用LINQ,但我还不会用,哈哈
int[] b = { 1, 4, 5, 7, 8, 9 }; ArrayList array = new ArrayList();
for (int i = 0; i < a.Length; i++)
{
if (!b.Contains(a[i]))
{
Console.WriteLine(a[i]);//找出2,3
}
}
for (int i = 0; i < b.Length; i++)
{
if (!a.Contains(b[i]))
{
Console.WriteLine(b[i]);//找出7,8
} } Console.ReadLine();