To get only the unique items in a list in C#, you can use the Distinct()
method from the System.Linq
namespace. Here's an example:
main.cs326 chars14 lines
This code will output:
main.cs10 chars6 lines
The Distinct()
method returns an IEnumerable<T>
that contains only the distinct elements of the input sequence, in this case, the numbers
list. We then convert this IEnumerable<T>
back to a list using the ToList()
method. Finally, we loop through the uniqueNumbers
list and output each item.
gistlibby LogSnag