Each item that is duplicated
List<String> duplicates = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
A list of all duplicates
var duplicates = lst.GroupBy(s => s)
.SelectMany(grp => grp.Skip(1));
Another Way
List<String> list = new List<String> { "6", "1", "2", "4", "6", "5", "1" };
var q = from s in list
group s by s into g
where g.Count() > 1
select g.First();
foreach (var item in q)
{
Console.WriteLine(item);
}
var arrString = "Hello Ronda, I'm calling you Ronda. How are you Ronda.".Split(" ".ToCharArray());
var duplicates = from wordCollection in arrString
group wordCollection by wordCollection into g
where g.Count() > 1
select new { g.Key, Count = g.Count() } ;
Comments