LINQ Distinct Operator

For example, we have one collection A = { 1, 2, 3, 3, 4, 4, 5}. In this collection 3 and 4 elements are repeated two times and we have to create a new sequence which takes these duplicates only one time. So we use distinct operator over A collection. C = A.distinct(); C = { 1, 2, 3, 4, 5}.

By default Distinct operator can not compare complex types. For this, we need to use second overload which takes an instance of IEqualityComparer. IEqualityComparer is used for comparing complex types. IEqualityCompare has two methods Equals and GetHashCode. Both methods are compulsory to implement.