Returns all elements of a collection that occur more than once in the given collection. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Returns all elements of a collection that occur more than once in the given collection.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;//from  w w  w .  ja  va2 s.c o m

public class Main{
        /// <summary>
      /// Returns all elements of a collection that occur more than once in the given collection.
      /// </summary>
      public static IEnumerable<T> Duplicates<T>(this IEnumerable<T> collection)
      {
         return collection.Where(item1 => collection.Count(item2 => item2.Equals(item1)) > 1);
      }
}

Related Tutorials