The answer of @Jodrell is very elegant (for me is the best), but only say depends of what you want the answer is correct
For the nex data
var testData = new List<List<int>>
{
new List<int> { 1, 2, 3 },
new List<int> { 1, 2, 3 },
new List<int> { 1, 2, 3, 3 },
}
The result is going to be:
Count | Set
3x | 1,2,3
And not the next:
Count | Set
2x | 1,2,3
1x | 1,2,3,3
So depends of your question...
Ok, so, with the last one this is my code, is not fancy and you can improve a lot of things
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestListInt
{
class Program
{
public class WrapperListInt
{
public List<int> list;
public WrapperListInt(List<int> list)
{
this.list = list;
}
public override int GetHashCode()
{
return 0;
}
public override bool Equals(object obj)
{
if (this == obj) return true;
WrapperListInt o = obj as WrapperListInt;
if (this.list.Count != o.list.Count) return false;
for (int i = 0; i < this.list.Count; i++)
{
if (this.list[i] != o.list[i]) { return false; }
}
return true;
}
}
public Program() {
var testData = new List<List<int>>
{
new List<int> { 1, 2, 3 },
new List<int> { 1, 3, 2 },
new List<int> { 1, 2, 3, 3 },
new List<int> { 6, 8, 3, 45,48 },
new List<int> { 9, 2, 15, 4 },
new List<int> { 9, 2, 4},
new List<int> { 9, 2, 4, 15 }
};
foreach (var td in testData)
{
td.Sort();
}
Dictionary<WrapperListInt, int> dic = new Dictionary<WrapperListInt, int>();
foreach (var listInt in testData)
{
WrapperListInt aux = new WrapperListInt(listInt);
int countList;
if (dic.TryGetValue(aux, out countList))
{
dic[aux]++;
}
else
{
dic.Add(aux, 1);
}
}
foreach (var d in dic)
{
var setString = String.Join(", ", d.Key.list);
Console.WriteLine($" {d.Value} | {setString}");
}
}
static void Main(string[] args)
{
new Program();
}
}
}
Linq
...TestData.Select(L => L.Sort()).GroupBy(x => x)
=> 返回的值是按列表分组的。 - Sebastian Siemens