count unique entries in a list in csharp

To count the unique entries in a list in C#, you can use the LINQ extension method Distinct() to get a new list containing only the unique values, and then use the Count() method to get the number of elements in that new list.

Here's an example:

main.cs
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int>() { 1, 2, 2, 3, 3, 3 };

        int uniqueCount = numbers.Distinct().Count();

        Console.WriteLine("The number of unique entries is: " + uniqueCount);
    }
}
334 chars
16 lines

This code defines a List<int> called numbers and initializes it with some integers. It then uses the Distinct() and Count() methods to count the number of unique entries in the list, which is 3. The result is printed to the console using Console.WriteLine().

related categories

gistlibby LogSnag