急... 如何比较两个数组中不相同的元素...

evjen 2009-12-19 09:53:53
数组

int[] a = {1,2,3,4,5,9};
int[] b = {1,4,5,7,8,9};


现在要计算出这两个数组中不相同的元素 : 2,3,7,8

谢谢各位...
...全文
1924 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
l576981437 2011-01-13
  • 打赏
  • 举报
回复


哦不 是前年的帖子
l576981437 2011-01-13
  • 打赏
  • 举报
回复


去年的帖子
吴青峰 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 qldsrx 的回复:]
不知道楼主是不是用的VS2008,我这里提供一个使用LINQ的高效算法:

C# code

using System.Linq;

int[] a = { 1, 2, 3, 4, 5, 9 };
int[] b = { 1, 4, 5, 7, 8, 9 };
var c = a.Cast<int>().Con……
[/Quote]

这个看起来只是一行代码,看是简单,但不一定是最高效的,如果直接排序比较的话,可能比这个更高效.....
YZX3288 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jiangshun 的回复:]
C# code
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)
……
[/Quote]
幫頂。。。。
ayan_smile 2010-12-06
  • 打赏
  • 举报
回复
good up
denbes 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 liyoubaidu 的回复:]
C# code
int[] result=a.Except(b).Concat(b.Except(a)).ToArray();
[/Quote]
up
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 zhangchuccc 的回复:]
引用 30 楼 zhangchuccc 的回复:

int[] a = { 1, 2, 3, 4, 5, 9 };
int[] b = { 1, 4, 5, 7, 8, 9 };

ArrayList array = new ArrayList();
for (int i = 0; i < a.Length; i++)
{
……

多打了个array,不好意思
[/Quote]
hhc123 2010-05-26
  • 打赏
  • 举报
回复
去年的贴了,还不结贴
zhangchuccc 2010-05-26
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 zhangchuccc 的回复:]

int[] a = { 1, 2, 3, 4, 5, 9 };
int[] b = { 1, 4, 5, 7, 8, 9 };

ArrayList array = new ArrayList();
for (int i = 0; i < a.Length; i++)
{
……
[/Quote]
多打了个array,不好意思
zhangchuccc 2010-05-26
  • 打赏
  • 举报
回复
int[] a = { 1, 2, 3, 4, 5, 9 };
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();
qq497525725 2010-05-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jiangshun 的回复:]
C# code
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)
……
[/Quote]

这个效率高.
andybang1981 2010-05-26
  • 打赏
  • 举报
回复
简单求结果的话只要双重循环即可
如果考虑性能的话就像楼上说的使用LINQ,但我还不会用,哈哈
pilipalalq 2010-05-26
  • 打赏
  • 举报
回复

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 就是了
Q先生199 2010-04-11
  • 打赏
  • 举报
回复

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

}
Q先生199 2010-04-11
  • 打赏
  • 举报
回复
写了个最蠢的方法...
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());
}

}
studyqs 2010-04-11
  • 打赏
  • 举报
回复
用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());

}

}
}
Snowdust 2010-04-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 litaoye 的回复:]
用归并写了一个。

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

……
[/Quote]

不知道LINQ的时间复杂度是多少,除开LINQ,归并排序应该是最高的了,T(n)=O(nlogn),其它几个都是O(n^2)。当然,就这么几个数讨论时间复杂度似乎有点多余。
兔子-顾问 2010-04-11
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 qldsrx 的回复:]

不知道楼主是不是用的VS2008,我这里提供一个使用LINQ的高效算法:
[C# code]
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());
}
[/code]
[/Quote]

+1 很好很强大。赞一个。
Teng_s2000 2010-04-11
  • 打赏
  • 举报
回复
qldsrx 2009-12-20
  • 打赏
  • 举报
回复
不知道楼主是不是用的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());
}
加载更多回复(19)

110,556

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧