Webservis Sites e Sistemas Web

Reconhecer itens duplicados em uma lista

var list = new List<string> { "a", "a", "b", "b", "r", "t" };

var distinct = new HashSet<string>();    
var duplicates = new HashSet<string>();

foreach (var s in list)
    if (!distinct.Add(s))
        duplicates.Add(s);

// distinct == { "a", "b", "r", "t" }
// duplicates == { "a", "b" }

Só os duplicados

List<string> list = new List<string>() { "a", "a", "b", "b", "r", "t" };

var dups = list.GroupBy(x => x)
    .Where(x => x.Count() > 1)
    .Select(x => x.Key)
    .ToList();

Fonte:
https://pt.stackoverflow.com/questions/56086/reconhecer-itens-duplicados-em-uma-lista

Sair da versão mobile