How to use Distinct operator?
The Distinct operator returns a new collection that contains only the unique elements from the original collection, based on the default equality comparer or a custom one. The Distinct operator is an extension method that can be applied to any type that implements IEnumerable<T>, such as arrays, lists, dictionaries, etc. For example, if you have an array of integers with some duplicates, you can use the Distinct operator to get a new array with only the distinct values:
int[] numbers = { 1, 2, 3, 4, 4, 5, 6, 6, 7 };
int[] distinctNumbers = numbers.Distinct().ToArray();
// distinctNumbers = { 1, 2, 3, 4, 5, 6, 7 }
You can also use a custom equality comparer to define how the elements are compared for equality. For example, if you have a list of strings with some duplicates that differ only by case, you can use the Distinct operator with a custom comparer that ignores the case:
List<string> names = new List<string> { "Alice", "Bob", "bob", "Charlie", "alice" };
List<string> distinctNames = names.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
// distinctNames = { "Alice", "Bob", "Charlie" }