To count duplicates in a collection in C#, you can use LINQ to group the elements by their value, and then count the number of items in each group greater than one. Here's an example using a List<string>
:
main.cs449 chars12 lines
This will output:
main.cs47 chars3 lines
This approach can be used with any type of collection and any type of element, as long as the element type implements Equals
and GetHashCode
correctly.
gistlibby LogSnag