duplicate list c# code example

Example 1: c# find duplicates in list

var query = lst.GroupBy(x => x)
              .Where(g => g.Count() > 1)
              .Select(y => y.Key)
              .ToList();

Example 2: list clone - C#

static class Extensions
{
    public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
    {
        return listToClone.Select(item => (T)item.Clone()).ToList();
    }
}

Example 3: copy a list C#

using System;
using System.Linq;
using System.Collections.Generic;

List<string> source = new List<string>() { "A", "B", "C" };
 
List<string> clonedList = source.ToList();
Console.WriteLine(String.Join(",", clonedList));

// Result = A,B,C