You can also use countto examine the linq query, see http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
in your case
entities.Where(p => entities.Count(x => x.Equals(p)) > 1);
the order is higher than the request O(n^2)
but you can do it simply using the following code inO(n log(n))
entities.Sort();
List<x> repeatedItems = new List<x>();
if (entities.Count > 1)
{
if (entities[0].Equals(entities[1]))
{
repeatedItems.Add(entities[0]);
}
}
for (int i=0;i<entities.Count;i++)
{
if (i < entities.Count -1)
{
if (entities[i].Equals(entities[i+1]))
{
repeatedItems.Add(entities[i+1]);
}
}
source
share