代码示例
//使用distinct()
List<string> lst1 = new List<string>()
{ "as", "lio", "sdrf", "asd", "lio" };
var reslst =lst1.Distinct().ToList();
public static List<T> RemoveT<T>(List<T> items)
{
HashSet<T> set = new HashSet<T>();
var res = new List<T>();//返回
for (int i = 0; i < items.Count; i++)
{
if (!set.Contains(items[i]))
{
set.Add(items[i]);
res.Add(items[i]);
}
}
return res;
}
public static List<string> ForLoopRemove(List<string> items)
{
List<string> output = new List<string>();
for (int i = 0; i < items.Count; i++)
{
bool flag = false;
//每个元素都与其他这个元素前面的比较,如果前面没有,则添加,否则不添加
for (int z = 0; z < i; z++)
{
if (items[z] == items[i])
{
flag = true;
break;
}
}
if (!flag)
{
output.Add(items[i]);
}
}
return output;
}
测试三种方式的运行速度
代码示例
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
//list 去重,三种方法
for (int test = 0; test < 3; test++)
{
// 获取测试数据
var testData = GetTestData(test);
var max = testData.Item3;///执行次数
var s1 = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
// 方法 1: use Distinct.
var unique = testData.Item2.Distinct().ToList();
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
// 方法 2: use HashSet.
var unique = Method.RemoveT(testData.Item2);
}
s2.Stop();
var s3 = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
// 方法 3: use nested for-loop.
var unique = Method.ForLoopRemove(testData.Item2);
}
s3.Stop();
// 打印信息.
Console.WriteLine(testData.Item1);
//调用时间.
Console.WriteLine(s1.Elapsed.TotalMilliseconds + " ms");
Console.WriteLine(s2.Elapsed.TotalMilliseconds + " ms");
Console.WriteLine(s3.Elapsed.TotalMilliseconds + " ms");
}
//结论: 当数据量少时,直接使用for-loop 遍历数据即可;
//当数据量大时,使用hashSet 或者时linq的distinct()方法
List<string> testLst= GetListWithDuplicates(100, 20);
Method.RemoveRepeat(testLst);
//实现按照对象的某个字段去重。
List<Student> lst = new List<Student>();
lst.Add(new Student { No = 23, name = "李磊" });
lst.Add(new Student { No = 45, name = "david" });
lst.Add(new Student { No = 12, name = "lily" });
lst.Add(new Student { No = 23, name = "李磊" });
lst.Add(new Student { No = 45, name = "david" });
lst.Add(new Student { No = 72, name = "lily" });
var res= lst.Distinct(new ItemEqualityComparer());
foreach (var item in res)
{
Console.WriteLine($"distinct: {item.No}---{item.name}");
}
Console.ReadKey();
}
///
/// 生成测试数据,最后的一个参数,表示方法被调用的次数
///
///
///
static Tuple<string, List<string>, int> GetTestData(int test)
{
// Tuple contains description string, list, the unique element count, and iterations for test.
switch (test)
{
default:
case 0:
return new Tuple<string, List<string>, int>("10 ELEMENT LIST, 0 DUPLICATES",
GetListWithDuplicates(10, 0),
100000);
case 1:
return new Tuple<string, List<string>, int>("300 ELEMENT LIST, 100 DUPLICATES",
GetListWithDuplicates(200, 100),
1000);
case 2:
return new Tuple<string, List<string>, int>("3000 ELEMENT LIST, 1000 DUPLICATES",
GetListWithDuplicates(2000, 1000),
100);
}
}
public static List<string> GetListWithDuplicates(int len, int repeatNum)
{
const string duplicateString = "bird";
List<string> result = new List<string>();
for (int i = 0; i < len; i++)
{
result.Add("cat" + i);
if (repeatNum > 0)
{
result.Add(duplicateString);
repeatNum--;
}
}
for (int i = 0; i < repeatNum; i++)
{
result.Add(duplicateString);
}
return result;
}
}
/// //
#region 按照对象的某个字段去重
public class Student
{
public string name;
public int No;
}
///
/// 按照学号判断
///
public class ItemEqualityComparer : IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
{
return x.No == y.No;
}
public int GetHashCode(Student obj)
{
return obj.No.GetHashCode();
}
}
#endregion
}
调用方法的封装
using System;
using System.Collections.Generic;
namespace ConsoleApp2
{
public static class Method
{
///
/// 使用两个循环,但是在数据量大时,这种方法比较慢
///
///
///
public static List<string> ForLoopRemove(List<string> items)
{
List<string> output = new List<string>();
for (int i = 0; i < items.Count; i++)
{
bool flag = false;
//每个元素都与其他这个元素前面的比较,如果前面没有,则添加,否则不添加
for (int z = 0; z < i; z++)
{
if (items[z] == items[i])
{
flag = true;
break;
}
}
if (!flag)
{
output.Add(items[i]);
}
}
return output;
}
///
/// 使用hashset去重
///
///
public static void RemoveRepeat(List<string> lst)
{
Console.WriteLine("intput :" + lst.Count);
HashSet<string> set = new HashSet<string>();
foreach (string item in lst)
{
set.Add(item);
}
Console.WriteLine(set.Count);
lst.Clear();
lst.AddRange(set);
Console.WriteLine("outPut: "+lst.Count);
}
///
/// 使用hashset去重的泛型方法
///
///
public static List<T> RemoveT<T>(List<T> items)
{
HashSet<T> set = new HashSet<T>();
var res = new List<T>();//返回
for (int i = 0; i < items.Count; i++)
{
if (!set.Contains(items[i]))
{
set.Add(items[i]);
res.Add(items[i]);
}
}
return res;
}
}
}