110,533
社区成员
发帖
与我相关
我的任务
分享
public class CustomIntListEqualityComparer : IEqualityComparer<List<int>>
{
public bool Equals(List<int> x, List<int> y)
{
//x==y==null
if (x == y)
return true;
if (x == null || y == null)
return false;
if (x.Count != y.Count)
return false;
for (int i = 0, count = x.Count; i < count; i++)
{
if (x[i] != y[i])
return false;
}
return true;
//throw new NotImplementedException();
}
public int GetHashCode(List<int> obj)
{
if (obj == null)
return 0;
int hash = 17;
hash = (hash + obj.Count) * 3;
return obj.Aggregate(hash, (start, next) => (hash = (hash + next) * 3));
//throw new NotImplementedException();
}
}
List<List<int>> list = new List<List<int>>()
{
new List<int>() { 0, 1, 2 },
new List<int>() { 0, 2, 1 },
new List<int>() { 2, 1, 0 },
new List<int>() { 0, 1, 2 },
new List<int>() { 2, 0, 1 },
new List<int>() { 1, 3, 8 }
};
foreach (var r in list.Distinct(new CustomIntListEqualityComparer()))
{
Console.WriteLine(string.Join(",", r.Select(e => string.Format("\"{0}\"", e.ToString()))));
}
var list = new List<List<int>>
{
new List<int> {0, 1, 2},
new List<int> {0, 1, 2},
new List<int> {1, 3, 8}
};
list = list.Distinct(new MyListComparer()).ToList();
internal class MyListComparer : IEqualityComparer<List<int>>
{
#region IEqualityComparer<List<int>> Members
public bool Equals(List<int> x, List<int> y)
{
x.Sort();
y.Sort();
if (x.Count != y.Count)
return false;
return !x.Where((t, index) => y[index] != t).Any();
}
public int GetHashCode(List<int> obj)
{
return obj.Aggregate(1, (current, i) => current*i);
}
#endregion
}
list = list.Distinct(new StringListComparer());
class StringListComparer : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
return string.Join(",", x.OrderBy(a => a)) == string.Join(",", y.OrderBy(a => a));
}
public int GetHashCode(List<string> obj)
{
return string.Join(",", x.OrderBy(a => a)).GetHashCode();
}
}
/// <summary>
/// 把List<int>转换成string
/// </summary>
/// <param name="listint"></param>
/// <returns></returns>
string getStringByInt(List<int> listint)
{
string str = "";
foreach (int item in listint)
{
str += item.ToString();
}
return str;
}
/// <summary>
/// 判断ListListint是否包含str字符串
/// </summary>
/// <param name="listnew"></param>
/// <param name="str"></param>
/// <returns></returns>
bool iscon(List<List<int>> listnew,string str)
{
foreach (List<int> item in listnew)
{
if (getStringByInt(item) == str)
{
return true;
}
}
return false;
}
List<List<int>> list = new List<List<int>>()
{
new List<int>() { 0, 1, 2 },
new List<int>() { 0, 1, 2 },
new List<int>() { 1, 3,8 }
};
List<List<int>> listnew = new List<List<int>>();
foreach (List<int> item in list)
{
string str = getStringByInt(item);
if (!iscon(listnew, str))
{
listnew.Add(item);
}
}
MessageBox.Show("listnew数目:" + listnew.Count);
List<List<int>> list = new List<List<int>>()
{
new List<int>() { 0, 1, 2 },
new List<int>() { 0, 1, 2 },
new List<int>() { 1, 3,8 }
};
List<List<int>> listnew = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (iscon(listnew, list[i]))//自己写个iscon方法判断list[i]是否在listnew内
{
list.Remove(list[i]);
}
else
{
listnew.Add(list[i]);
}
}
List<List<int>> list = new List<List<int>>()
{
new List<int>() { 0, 1, 2 },
new List<int>() { 0, 1, 2 },
new List<int>() { 1, 3,8 }
};
List<List<int>> list1 = new List<List<int>>();
foreach(List<int> list0 in list)
{
if (!list1.Contains(list0))
{
list1.Add(list0);
}
}
List<List<int>> list1 = new List<List<int>>();
foreach(List<int> list0 in list1)
{
if (!list1.Contains(list0))
{
list1.Add(list0);
}
}