Поиск дубликатов без LINQ - C#
Формулировка задачи:
Есть список объектов класса Class1.
т.е. List<Class1>
Нужно вывести один раз ID объекта, который имеет дубликаты, и все Num, где он дублируется. И так для всех объектов
Т.е. вид примерно такой - asdbtr = 1,5,8,9; dsabte = 2,12,54,75 ...
Помогите решить, пожалуйста
public class Class1
{
public int Num; //номер по порядку
public string ID; //идентификатор
}Решение задачи: «Поиск дубликатов без LINQ»
textual
Листинг программы
// using System;
// using System.Collections.Generic;
List<Class1> sourceList = ...; // заполненный список
Dictionary<string, List<int>> groups = new Dictionary<string, List<int>>();
for (int index = 0; index < sourceList.Count; ++index)
{
Class1 current = sourceList[index];
if (groups.ContainsKey(current.ID))
{
groups[current.ID].Add(index);
}
else
{
List<int> indices = new List<int>(4);
indices.Add(index);
groups[current.ID] = indices;
}
}
foreach (var entry in groups)
{
Console.Write(entry.Key);
Console.Write(" : ");
var indices = entry.Value;
for (int index = 0; index < indices.Count; ++index)
{
Console.Write(indices[index]);
if (index != (indices.Count - 1))
{
Console.Write(", ");
}
}
Console.Write("; ");
}