Preview:
// FOREACH
public static bool ContainsDuplicates < T > ( this IEnumerable < T > enumerable )
{
    HashSet < T > knownElements = new ( ) ;
    foreach ( T element in enumerable )
   {
     if ( ! knownElements.Add ( element ) )
       {
         return true ;
       }
     
    }                              
    return false ;
}
//HashSet . Add returns false if the element is already present

//LINQ ANY
public static bool ContainsDuplicates < T > ( this IEnumerable < T > enumerable )
{ 
    HashSet < T > knownElements = new ( ) ;
    return enumerable.Any(element => !knownElements.Add(element));
}

//LINQ ALL
public static bool ContainsDuplicates < T > ( this IEnumerable < T > enumerable )
{ 
    HashSet < T > knownElements = new ( ) ;
    return !enumerable.All(knownElements.Add);
}


// primena
List < int > numbers = new ( ) { 1 , 2 , 3 , 3 , 4 , 5 } ;
  if ( numbers.ContainsDuplicates ( ) )
{  
    // Do something when there are duplicates
}

                                               
                                               
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter