Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# list remove duplicate items

List<string> lstWhitDuplicate = new list<string>();
lstWhitDuplicate.add("hallo");
lstWhitDuplicate.add("hallo");
lstWhitDuplicate.add("World");

List<string> lst = lstWhitDuplicate.Distinct().ToList(); 

// output lstWhitDuplicate:
// hallo hallo world
// output lst:
// hallo word
Comment

c# remove duplicates from list

List<T> withDupes = LoadSomeData();
List<T> noDupes = withDupes.Distinct().ToList();
Comment

Remove duplicates from a List in C#

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        HashSet<int> evenNumbers = new HashSet<int>();
        HashSet<int> oddNumbers = new HashSet<int>();

        for (int i = 0; i < 5; i++)
        {
            // Populate numbers with just even numbers.
            evenNumbers.Add(i * 2);

            // Populate oddNumbers with just odd numbers.
            oddNumbers.Add((i * 2) + 1);
        }

        Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
        DisplaySet(evenNumbers);

        Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);
        DisplaySet(oddNumbers);

        // Create a new HashSet populated with even numbers.
        HashSet<int> numbers = new HashSet<int>(evenNumbers);
        Console.WriteLine("numbers UnionWith oddNumbers...");
        numbers.UnionWith(oddNumbers);

        Console.Write("numbers contains {0} elements: ", numbers.Count);
        DisplaySet(numbers);
    }

    private static void DisplaySet(HashSet<int> set)
    {
        Console.Write("{");
        foreach (int i in set)
        {
            Console.Write(" {0}", i);
        }
        Console.WriteLine(" }");
    }
}

/* This example produces output similar to the following:
 * evenNumbers contains 5 elements: { 0 2 4 6 8 }
 * oddNumbers contains 5 elements: { 1 3 5 7 9 }
 * numbers UnionWith oddNumbers...
 * numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
 */
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# encrypted 
Csharp :: how to add to a list only items that are not already in the list c# 
Csharp :: unity onclick object 
Csharp :: default parameter c# 
Csharp :: random mac address c# 
Csharp :: How to post request C# with returning responsebody 
Csharp :: simple code to call rest api c# 
Csharp :: Sort ListBox numerically in C# 
Csharp :: All Possible SubString 
Csharp :: C# fileinfo creation date 
Csharp :: c# pi 
Csharp :: get the number of cpu c# 
Csharp :: dataannotations datetime range 
Csharp :: C# round number of digits after decimal point 
Csharp :: parametrizedthreadstart C# 
Csharp :: array declaration in c# 
Csharp :: c# Program to check if a given year is leap year 
Csharp :: ActionExecutingContext result response return 
Csharp :: remove item from list in for loop c# 
Csharp :: Commenting on C# 
Csharp :: c# inheritance 
Csharp :: delete all rows from table mvc 
Csharp :: animation setbool unity 
Csharp :: Code to disable Debug.log 
Csharp :: combine two arraylist c# 
Csharp :: how to sign in with your unity id in unity hub 
Csharp :: serenity frameword order column 
Csharp :: c# instance class with ilogger 
Csharp :: clickable table row asp.net core 
Csharp :: Send Hotmail/Outlook Email C# (Win/ASP.NET) 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =