比如
List<string> a=new List<string>();
a里面有{111,222,333,444,555,666,777,888,999,111,222,333,444}
找出交集得到结果是
111,222,333,444速度要快的,数据量是在百万条之上。什么算法最快?
List<string> a=new List<string>();
a里面有{111,222,333,444,555,666,777,888,999,111,222,333,444}
找出交集得到结果是
111,222,333,444速度要快的,数据量是在百万条之上。什么算法最快?
解决方案 »
- 自动更新功能
- 如何将C#中绘制的图形(如一个三角形)以EmF矢量格式保存
- C# winform 运算符后缺少操作数
- TableTableAdapter.Fill 和progressBar
- C# .Net CS 程序 怎样才能在简体中文版系统和繁体中文版系统上同时兼容运行?
- 初学者-session与viewstate有什么不同?
- 求助:vba代码改成c#不起作用
- 打包后安装,在win200下能创建数据库而在winxp下不行
- 如何给textbox控件发送按键消息
- 请问如何取得当前运行的程序实例句柄?(急,在线给分)
- 朋友们来讲讲AspNetPager的使用详细过程
- 有在Mac OS 上有开发经验的请进
List<string> list = a.Where(s => a.Count(s2 => s == s2) >= 2).Distinct().ToList();
foreach(string s in a)
{
if(dic.ContainsKey(s))
dic[s]++;
else
dic.Add(s,1);
}
List<string> list = new List<string>();
foreach(string s in dic)
{
if(dic[s]>1)
list.Add(s);
}
//写了一个DEMO 测试了一下不同的方法所有的时间
//DEMO中的LIST中有100条数据 每个方法重复执行100次
void Main()
{
InitList();
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
for(int i=1;i<=100;i++)
{
GetLINQFunctionTestTime();
}
stopwatch.Stop();
Console.WriteLine("使用 LINQ 总共用时 {0} 毫秒,平均 {1}毫秒/次",stopwatch.ElapsedTicks,stopwatch.ElapsedTicks/100);
stopwatch.Start();
for(int i=1;i<=100;i++)
{
GetOtherFunctionTestTime();
}
stopwatch.Stop();
Console.WriteLine("使用 Other 总共用时 {0} 毫秒,平均 {1}毫秒/次",stopwatch.ElapsedTicks,stopwatch.ElapsedTicks/100);
stopwatch.Start();
for(int i=1;i<=100;i++)
{
GetDictionaryFunctionTestTime();
}
stopwatch.Stop();
Console.WriteLine("使用 Dictionary 总共用时 {0} 毫秒,平均 {1}毫秒/次",stopwatch.ElapsedTicks,stopwatch.ElapsedTicks/100);
/*
使用 LINQ 总共用时 7344 毫秒,平均 73毫秒/次
使用 Other 总共用时 15501 毫秒,平均 155毫秒/次
使用 Dictionary 总共用时 22191 毫秒,平均 221毫秒/次 */
}void GetLINQFunctionTestTime()
{
var query=list.GroupBy(l=>l).Where(g=>g.Count()>1).Select(g=>g.Key).ToList();
}void GetOtherFunctionTestTime()
{
ArrayList ay=new ArrayList();
List<int> result=new List<int>();
foreach(int str in list)
{
if(ay.Contains(str))
{
result.Add(str);
}
else
{
ay.Add(str);
}
}
}void GetDictionaryFunctionTestTime()
{
Dictionary<int,int> dic = new Dictionary<int,int>();
foreach(int s in list)
{
if(dic.ContainsKey(s))
dic[s]++;
else
dic.Add(s,1);
}
List<int> result = new List<int>();
foreach(var s in dic)
{
if(dic[s.Key]>1)
result.Add(s.Key);
}}
public List<int> list = new List<int>();
void InitList()
{
var list1=Enumerable.Range(10,50); // 10 - 59
var list2=Enumerable.Range(20,20); // 20 - 39
var list3=Enumerable.Range(40,30); // 40 - 69
list=list1.Concat(list2).Concat(list3).ToList();
}