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# error CS0515 
Csharp :: discord bot status code c# 
Csharp :: c# list append 
Csharp :: asp.net core identity get user id 
Csharp :: how to reference a child object unity 
Csharp :: unity new Color() 
Csharp :: if char is upper csharp 
Csharp :: loop over enum values 
Csharp :: get working directory c# 
Csharp :: how to print c# 
Csharp :: 2d game art 
Csharp :: write text files with C# 
Csharp :: get hard disk serial number 
Csharp :: No context type was found in the assembly 
Csharp :: convert json to list object c# 
Csharp :: create material unity script 
Csharp :: asp net c# compare date to current 
Csharp :: how to access individual characters in a string in c# 
Csharp :: how to create a file through c# script 
Csharp :: c# build string out of list of strings 
Csharp :: c# stop process 
Csharp :: unity mouse click position 
Csharp :: random.range unity not working 
Csharp :: how to convert int to float in c# 
Csharp :: switch case c# contains 
Csharp :: Play Sound c# 
Csharp :: get device connected to player input unity 
Csharp :: when was the third world war 
Csharp :: how to open website from c# program 
Csharp :: array to list c 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =