<p>Anyone have a quick method for de-duplicating a generic List in C#?</p>

[{'answer_id': 47755, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 6, 'selected': False, 'text': "<p>Sort it, then check two and two next to each others, as the duplicates will clump together.</p>\n\n<p>Something like this:</p>\n\n<pre><code>list.Sort();\nInt32 index = list.Count - 1;\nwhile (index &gt; 0)\n{\n    if (list[index] == list[index - 1])\n    {\n        if (index &lt; list.Count - 1)\n            (list[index], list[list.Count - 1]) = (list[list.Count - 1], list[index]);\n        list.RemoveAt(list.Count - 1);\n        index--;\n    }\n    else\n        index--;\n}\n</code></pre>\n\n<p>Notes:</p>\n\n<ul>\n<li>Comparison is done from back to front, to avoid having to resort list after each removal</li>\n<li>This example now uses C# Value Tuples to do the swapping, substitute with appropriate code if you can't use that</li>\n<li>The end-result is no longer sorted</li>\n</ul>\n"}, {'answer_id': 47757, 'author': 'Jason Baker', 'author_id': 2147, 'author_profile': 'https://Stackoverflow.com/users/2147', 'pm_score': 9, 'selected': True, 'text': '<p>Perhaps you should consider using a <a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx" rel="noreferrer">HashSet</a>.</p>\n\n<p>From the MSDN link:</p>\n\n<pre><code>using System;\nusing System.Collections.Generic;\n\nclass Program\n{\n    static void Main()\n    {\n        HashSet&lt;int&gt; evenNumbers = new HashSet&lt;int&gt;();\n        HashSet&lt;int&gt; oddNumbers = new HashSet&lt;int&gt;();\n\n        for (int i = 0; i &lt; 5; i++)\n        {\n            // Populate numbers with just even numbers.\n            evenNumbers.Add(i * 2);\n\n            // Populate oddNumbers with just odd numbers.\n            oddNumbers.Add((i * 2) + 1);\n        }\n\n        Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);\n        DisplaySet(evenNumbers);\n\n        Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);\n        DisplaySet(oddNumbers);\n\n        // Create a new HashSet populated with even numbers.\n        HashSet&lt;int&gt; numbers = new HashSet&lt;int&gt;(evenNumbers);\n        Console.WriteLine("numbers UnionWith oddNumbers...");\n        numbers.UnionWith(oddNumbers);\n\n        Console.Write("numbers contains {0} elements: ", numbers.Count);\n        DisplaySet(numbers);\n    }\n\n    private static void DisplaySet(HashSet&lt;int&gt; set)\n    {\n        Console.Write("{");\n        foreach (int i in set)\n        {\n            Console.Write(" {0}", i);\n        }\n        Console.WriteLine(" }");\n    }\n}\n\n/* This example produces output similar to the following:\n * evenNumbers contains 5 elements: { 0 2 4 6 8 }\n * oddNumbers contains 5 elements: { 1 3 5 7 9 }\n * numbers UnionWith oddNumbers...\n * numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }\n */\n</code></pre>\n'}, {'answer_id': 47761, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 4, 'selected': False, 'text': '<p>In Java (I assume C# is more or less identical):</p>\n\n<pre><code>list = new ArrayList&lt;T&gt;(new HashSet&lt;T&gt;(list))\n</code></pre>\n\n<p>If you really wanted to mutate the original list:</p>\n\n<pre><code>List&lt;T&gt; noDupes = new ArrayList&lt;T&gt;(new HashSet&lt;T&gt;(list));\nlist.clear();\nlist.addAll(noDupes);\n</code></pre>\n\n<p>To preserve order, simply replace HashSet with LinkedHashSet.</p>\n'}, {'answer_id': 47763, 'author': 'Motti', 'author_id': 3848, 'author_profile': 'https://Stackoverflow.com/users/3848', 'pm_score': 3, 'selected': False, 'text': '<p>If you don\'t care about the order you can just shove the items into a <code>HashSet</code>, if you <em>do</em> want to maintain the order you can do something like this:</p>\n<pre><code>var unique = new List&lt;T&gt;();\nvar hs = new HashSet&lt;T&gt;();\nforeach (T t in list)\n    if (hs.Add(t))\n        unique.Add(t);\n</code></pre>\n<p>Or the Linq way:</p>\n<pre><code>var hs = new HashSet&lt;T&gt;();\nlist.All( x =&gt;  hs.Add(x) );\n</code></pre>\n<p><strong>Edit:</strong> The <code>HashSet</code> method is <code>O(N)</code> time and <code>O(N)</code> space while sorting and then making unique (as suggested by @<a href="https://stackoverflow.com/questions/47752/remove-duplicates-from-a-listt-in-c#47755">lassevk</a> and others) is <code>O(N*lgN)</code> time and <code>O(1)</code> space so it\'s not so clear to me (as it was at first glance) that the sorting way is inferior</p>\n'}, {'answer_id': 47776, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 8, 'selected': False, 'text': '<p>How about:</p>\n\n<pre><code>var noDupes = list.Distinct().ToList();\n</code></pre>\n\n<p>In .net 3.5?</p>\n'}, {'answer_id': 47777, 'author': 'Factor Mystic', 'author_id': 1569, 'author_profile': 'https://Stackoverflow.com/users/1569', 'pm_score': 10, 'selected': False, 'text': "<p>If you're using .Net 3+, you can use Linq.</p>\n\n<pre><code>List&lt;T&gt; withDupes = LoadSomeData();\nList&lt;T&gt; noDupes = withDupes.Distinct().ToList();\n</code></pre>\n"}, {'answer_id': 48275, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 5, 'selected': False, 'text': "<p>As kronoz said in .Net 3.5 you can use <code>Distinct()</code>.</p>\n\n<p>In .Net 2 you could mimic it:</p>\n\n<pre><code>public IEnumerable&lt;T&gt; DedupCollection&lt;T&gt; (IEnumerable&lt;T&gt; input) \n{\n    var passedValues = new HashSet&lt;T&gt;();\n\n    // Relatively simple dupe check alg used as example\n    foreach(T item in input)\n        if(passedValues.Add(item)) // True if item is new\n            yield return item;\n}\n</code></pre>\n\n<p>This could be used to dedupe any collection and will return the values in the original order.</p>\n\n<p>It's normally much quicker to filter a collection (as both <code>Distinct()</code> and this sample does) than it would be to remove items from it.</p>\n"}, {'answer_id': 1792582, 'author': 'Even Mien', 'author_id': 73794, 'author_profile': 'https://Stackoverflow.com/users/73794', 'pm_score': 7, 'selected': False, 'text': '<p><strong>Simply initialize a HashSet with a List of the same type:</strong></p>\n\n<pre><code>var noDupes = new HashSet&lt;T&gt;(withDupes);\n</code></pre>\n\n<p><strong>Or, if you want a List returned:</strong></p>\n\n<pre><code>var noDupsList = new HashSet&lt;T&gt;(withDupes).ToList();\n</code></pre>\n'}, {'answer_id': 2571314, 'author': 'Geoff Taylor', 'author_id': 308300, 'author_profile': 'https://Stackoverflow.com/users/308300', 'pm_score': 4, 'selected': False, 'text': '<p>An extension method might be a decent way to go... something like this:</p>\n\n<pre><code>public static List&lt;T&gt; Deduplicate&lt;T&gt;(this List&lt;T&gt; listToDeduplicate)\n{\n    return listToDeduplicate.Distinct().ToList();\n}\n</code></pre>\n\n<p>And then call like this, for example:</p>\n\n<pre><code>List&lt;int&gt; myFilteredList = unfilteredList.Deduplicate();\n</code></pre>\n'}, {'answer_id': 4954239, 'author': 'Bhasin', 'author_id': 610952, 'author_profile': 'https://Stackoverflow.com/users/610952', 'pm_score': 2, 'selected': False, 'text': '<p>Another way in .Net 2.0</p>\n\n<pre><code>    static void Main(string[] args)\n    {\n        List&lt;string&gt; alpha = new List&lt;string&gt;();\n\n        for(char a = \'a\'; a &lt;= \'d\'; a++)\n        {\n            alpha.Add(a.ToString());\n            alpha.Add(a.ToString());\n        }\n\n        Console.WriteLine("Data :");\n        alpha.ForEach(delegate(string t) { Console.WriteLine(t); });\n\n        alpha.ForEach(delegate (string v)\n                          {\n                              if (alpha.FindAll(delegate(string t) { return t == v; }).Count &gt; 1)\n                                  alpha.Remove(v);\n                          });\n\n        Console.WriteLine("Unique Result :");\n        alpha.ForEach(delegate(string t) { Console.WriteLine(t);});\n        Console.ReadKey();\n    }\n</code></pre>\n'}, {'answer_id': 5114231, 'author': 'gary', 'author_id': 63844, 'author_profile': 'https://Stackoverflow.com/users/63844', 'pm_score': 3, 'selected': False, 'text': "<p>Here's an extension method for removing adjacent duplicates in-situ. Call Sort() first and pass in the same IComparer. This should be more efficient than Lasse V. Karlsen's version which calls RemoveAt repeatedly (resulting in multiple block memory moves).</p>\n\n<pre><code>public static void RemoveAdjacentDuplicates&lt;T&gt;(this List&lt;T&gt; List, IComparer&lt;T&gt; Comparer)\n{\n    int NumUnique = 0;\n    for (int i = 0; i &lt; List.Count; i++)\n        if ((i == 0) || (Comparer.Compare(List[NumUnique - 1], List[i]) != 0))\n            List[NumUnique++] = List[i];\n    List.RemoveRange(NumUnique, List.Count - NumUnique);\n}\n</code></pre>\n"}, {'answer_id': 5609895, 'author': 'Ravi Ganesan', 'author_id': 700589, 'author_profile': 'https://Stackoverflow.com/users/700589', 'pm_score': 2, 'selected': False, 'text': "<p>There are many ways to solve - the duplicates issue in the List, below is one of them:</p>\n\n<pre><code>List&lt;Container&gt; containerList = LoadContainer();//Assume it has duplicates\nList&lt;Container&gt; filteredList = new  List&lt;Container&gt;();\nforeach (var container in containerList)\n{ \n  Container duplicateContainer = containerList.Find(delegate(Container checkContainer)\n  { return (checkContainer.UniqueId == container.UniqueId); });\n   //Assume 'UniqueId' is the property of the Container class on which u r making a search\n\n    if(!containerList.Contains(duplicateContainer) //Add object when not found in the new class object\n      {\n        filteredList.Add(container);\n       }\n  }\n</code></pre>\n\n<p>Cheers\nRavi Ganesan</p>\n"}, {'answer_id': 9276761, 'author': 'David J.', 'author_id': 1209045, 'author_profile': 'https://Stackoverflow.com/users/1209045', 'pm_score': 2, 'selected': False, 'text': "<p>Here's a simple solution that doesn't require any hard-to-read LINQ or any prior sorting of the list.</p>\n\n<pre><code>   private static void CheckForDuplicateItems(List&lt;string&gt; items)\n    {\n        if (items == null ||\n            items.Count == 0)\n            return;\n\n        for (int outerIndex = 0; outerIndex &lt; items.Count; outerIndex++)\n        {\n            for (int innerIndex = 0; innerIndex &lt; items.Count; innerIndex++)\n            {\n                if (innerIndex == outerIndex) continue;\n                if (items[outerIndex].Equals(items[innerIndex]))\n                {\n                    // Duplicate Found\n                }\n            }\n        }\n    }\n</code></pre>\n"}, {'answer_id': 11255357, 'author': 'Chris', 'author_id': 1490135, 'author_profile': 'https://Stackoverflow.com/users/1490135', 'pm_score': 2, 'selected': False, 'text': '<p>Might be easier to simply make sure that duplicates are not added to the list.</p>\n\n<pre><code>if(items.IndexOf(new_item) &lt; 0) \n    items.add(new_item)\n</code></pre>\n'}, {'answer_id': 11694032, 'author': 'Eric', 'author_id': 803629, 'author_profile': 'https://Stackoverflow.com/users/803629', 'pm_score': 5, 'selected': False, 'text': '<p>I like to use this command:</p>\n<pre><code>List&lt;Store&gt; myStoreList = Service.GetStoreListbyProvince(provinceId)\n                                                 .GroupBy(s =&gt; s.City)\n                                                 .Select(grp =&gt; grp.FirstOrDefault())\n                                                 .OrderBy(s =&gt; s.City)\n                                                 .ToList();\n</code></pre>\n<p>I have these fields in my list: Id, StoreName, City, PostalCode\nI wanted to show list of cities in a dropdown which has duplicate values.\nsolution: Group by city then pick the first one for the list.</p>\n'}, {'answer_id': 13404160, 'author': 'Hossein Sarshar', 'author_id': 1202836, 'author_profile': 'https://Stackoverflow.com/users/1202836', 'pm_score': 5, 'selected': False, 'text': '<p>It worked for me. simply use</p>\n\n<pre><code>List&lt;Type&gt; liIDs = liIDs.Distinct().ToList&lt;Type&gt;();\n</code></pre>\n\n<p>Replace "Type" with your desired type e.g. int.</p>\n'}, {'answer_id': 19516222, 'author': 'Guest', 'author_id': 2906798, 'author_profile': 'https://Stackoverflow.com/users/2906798', 'pm_score': 2, 'selected': False, 'text': '<p>David J.\'s answer is a good method, no need for extra objects, sorting, etc. It can be improved on however:</p>\n\n<p><code>for (int innerIndex = items.Count - 1; innerIndex &gt; outerIndex ; innerIndex--)</code></p>\n\n<p>So the outer loop goes top bottom for the entire list, but the inner loop goes bottom "until the outer loop position is reached".</p>\n\n<p>The outer loop makes sure the entire list is processed, the inner loop finds the actual duplicates, those can only happen in the part that the outer loop hasn\'t processed yet.</p>\n\n<p>Or if you don\'t want to do bottom up for the inner loop you could have the inner loop start at outerIndex + 1.</p>\n'}, {'answer_id': 23652999, 'author': 'Paul Richards', 'author_id': 2282723, 'author_profile': 'https://Stackoverflow.com/users/2282723', 'pm_score': 1, 'selected': False, 'text': '<pre><code>  public static void RemoveDuplicates&lt;T&gt;(IList&lt;T&gt; list )\n  {\n     if (list == null)\n     {\n        return;\n     }\n     int i = 1;\n     while(i&lt;list.Count)\n     {\n        int j = 0;\n        bool remove = false;\n        while (j &lt; i &amp;&amp; !remove)\n        {\n           if (list[i].Equals(list[j]))\n           {\n              remove = true;\n           }\n           j++;\n        }\n        if (remove)\n        {\n           list.RemoveAt(i);\n        }\n        else\n        {\n           i++;\n        }\n     }  \n  }\n</code></pre>\n'}, {'answer_id': 27004677, 'author': 'Grant', 'author_id': 1618538, 'author_profile': 'https://Stackoverflow.com/users/1618538', 'pm_score': 3, 'selected': False, 'text': '<p>As a helper method (without Linq):</p>\n\n<pre><code>public static List&lt;T&gt; Distinct&lt;T&gt;(this List&lt;T&gt; list)\n{\n    return (new HashSet&lt;T&gt;(list)).ToList();\n}\n</code></pre>\n'}, {'answer_id': 42813216, 'author': 'dush88c', 'author_id': 5097602, 'author_profile': 'https://Stackoverflow.com/users/5097602', 'pm_score': 3, 'selected': False, 'text': '<p>Installing the <a href="https://morelinq.github.io/" rel="noreferrer">MoreLINQ</a> package via Nuget, you can easily distinct object list by a property </p>\n\n<pre><code>IEnumerable&lt;Catalogue&gt; distinctCatalogues = catalogues.DistinctBy(c =&gt; c.CatalogueCode); \n</code></pre>\n'}, {'answer_id': 45533433, 'author': 'flagamba', 'author_id': 2352444, 'author_profile': 'https://Stackoverflow.com/users/2352444', 'pm_score': 2, 'selected': False, 'text': '<p>You can use Union</p>\n\n<pre><code>obj2 = obj1.Union(obj1).ToList();\n</code></pre>\n'}, {'answer_id': 48767398, 'author': 'WonderWorker', 'author_id': 1271898, 'author_profile': 'https://Stackoverflow.com/users/1271898', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n  <p>Use Linq's <strong>Union</strong> method.</p>\n</blockquote>\n\n<p><em>Note: This solution requires no knowledge of Linq, aside from that it exists.</em></p>\n\n<p><strong>Code</strong></p>\n\n<p>Begin by adding the following to the top of your class file:</p>\n\n<pre><code>using System.Linq;\n</code></pre>\n\n<p>Now, you can use the following to remove duplicates from an object called, <code>obj1</code>:</p>\n\n<pre><code>obj1 = obj1.Union(obj1).ToList();\n</code></pre>\n\n<p><em>Note: Rename <code>obj1</code> to the name of your object.</em></p>\n\n<p><strong>How it works</strong></p>\n\n<ol>\n<li><p>The Union command lists one of each entry of two source objects. Since obj1 is both source objects, this reduces obj1 to one of each entry.</p></li>\n<li><p>The <code>ToList()</code> returns a new List. This is necessary, because Linq commands like <code>Union</code> returns the result as an IEnumerable result instead of modifying the original List or returning a new List.</p></li>\n</ol>\n"}, {'answer_id': 49916992, 'author': 'Moctar Haiz', 'author_id': 3142365, 'author_profile': 'https://Stackoverflow.com/users/3142365', 'pm_score': 2, 'selected': False, 'text': '<p>A simple intuitive implementation:</p>\n\n<pre><code>public static List&lt;PointF&gt; RemoveDuplicates(List&lt;PointF&gt; listPoints)\n{\n    List&lt;PointF&gt; result = new List&lt;PointF&gt;();\n\n    for (int i = 0; i &lt; listPoints.Count; i++)\n    {\n        if (!result.Contains(listPoints[i]))\n            result.Add(listPoints[i]);\n        }\n\n        return result;\n    }\n</code></pre>\n'}, {'answer_id': 53579622, 'author': 'Reza Jenabi', 'author_id': 9549856, 'author_profile': 'https://Stackoverflow.com/users/9549856', 'pm_score': 3, 'selected': False, 'text': '<p>If you have tow classes <code>Product</code> and <code>Customer</code> and we want to remove duplicate items from their list</p>\n\n<pre><code>public class Product\n{\n    public int Id { get; set; }\n    public string ProductName { get; set; }\n}\n\npublic class Customer\n{\n    public int Id { get; set; }\n    public string CustomerName { get; set; }\n\n}\n</code></pre>\n\n<p>You must define a generic class in the form below</p>\n\n<pre><code>public class ItemEqualityComparer&lt;T&gt; : IEqualityComparer&lt;T&gt; where T : class\n{\n    private readonly PropertyInfo _propertyInfo;\n\n    public ItemEqualityComparer(string keyItem)\n    {\n        _propertyInfo = typeof(T).GetProperty(keyItem, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);\n    }\n\n    public bool Equals(T x, T y)\n    {\n        var xValue = _propertyInfo?.GetValue(x, null);\n        var yValue = _propertyInfo?.GetValue(y, null);\n        return xValue != null &amp;&amp; yValue != null &amp;&amp; xValue.Equals(yValue);\n    }\n\n    public int GetHashCode(T obj)\n    {\n        var propertyValue = _propertyInfo.GetValue(obj, null);\n        return propertyValue == null ? 0 : propertyValue.GetHashCode();\n    }\n}\n</code></pre>\n\n<p>then, You can remove duplicate items in your list.</p>\n\n<pre><code>var products = new List&lt;Product&gt;\n            {\n                new Product{ProductName = "product 1" ,Id = 1,},\n                new Product{ProductName = "product 2" ,Id = 2,},\n                new Product{ProductName = "product 2" ,Id = 4,},\n                new Product{ProductName = "product 2" ,Id = 4,},\n            };\nvar productList = products.Distinct(new ItemEqualityComparer&lt;Product&gt;(nameof(Product.Id))).ToList();\n\nvar customers = new List&lt;Customer&gt;\n            {\n                new Customer{CustomerName = "Customer 1" ,Id = 5,},\n                new Customer{CustomerName = "Customer 2" ,Id = 5,},\n                new Customer{CustomerName = "Customer 2" ,Id = 5,},\n                new Customer{CustomerName = "Customer 2" ,Id = 5,},\n            };\nvar customerList = customers.Distinct(new ItemEqualityComparer&lt;Customer&gt;(nameof(Customer.Id))).ToList();\n</code></pre>\n\n<p>this code remove duplicate items by <code>Id</code> if you want remove duplicate items by other property, you can change <code>nameof(YourClass.DuplicateProperty)</code>  same <code>nameof(Customer.CustomerName)</code> then remove duplicate items by <code>CustomerName</code> Property.</p>\n'}, {'answer_id': 55140260, 'author': 'Alfred Udah', 'author_id': 6468488, 'author_profile': 'https://Stackoverflow.com/users/6468488', 'pm_score': 4, 'selected': False, 'text': '<p>This takes distinct (the elements without duplicating elements) and convert it into a list again:</p>\n\n<pre><code>List&lt;type&gt; myNoneDuplicateValue = listValueWithDuplicate.Distinct().ToList();\n</code></pre>\n'}, {'answer_id': 56344965, 'author': 'Darkgaze', 'author_id': 772739, 'author_profile': 'https://Stackoverflow.com/users/772739', 'pm_score': 2, 'selected': False, 'text': "<p>All answers copy lists, or create a new list, or use slow functions, or are just painfully slow.</p>\n\n<p>To my understanding, this is the <strong>fastest and cheapest method</strong> I know (also, backed by a very experienced programmer specialized on real-time physics optimization).</p>\n\n<pre><code>// Duplicates will be noticed after a sort O(nLogn)\nlist.Sort();\n\n// Store the current and last items. Current item declaration is not really needed, and probably optimized by the compiler, but in case it's not...\nint lastItem = -1;\nint currItem = -1;\n\nint size = list.Count;\n\n// Store the index pointing to the last item we want to keep in the list\nint last = size - 1;\n\n// Travel the items from last to first O(n)\nfor (int i = last; i &gt;= 0; --i)\n{\n    currItem = list[i];\n\n    // If this item was the same as the previous one, we don't want it\n    if (currItem == lastItem)\n    {\n        // Overwrite last in current place. It is a swap but we don't need the last\n       list[i] = list[last];\n\n        // Reduce the last index, we don't want that one anymore\n        last--;\n    }\n\n    // A new item, we store it and continue\n    else\n        lastItem = currItem;\n}\n\n// We now have an unsorted list with the duplicates at the end.\n\n// Remove the last items just once\nlist.RemoveRange(last + 1, size - last - 1);\n\n// Sort again O(n logn)\nlist.Sort();\n</code></pre>\n\n<p>Final cost is:</p>\n\n<p>nlogn + n + nlogn = n + 2nlogn = <strong>O(nlogn)</strong> which is pretty nice.</p>\n\n<p><em>Note about RemoveRange:</em>\nSince we cannot set the count of the list and avoid using the Remove funcions, I don't know exactly the speed of this operation but I guess it is the fastest way. </p>\n"}, {'answer_id': 63762013, 'author': 'user3150241', 'author_id': 3150241, 'author_profile': 'https://Stackoverflow.com/users/3150241', 'pm_score': 0, 'selected': False, 'text': '<p>I think the simplest way is:</p>\n<p>Create a new list and add unique item.</p>\n<p>Example:</p>\n<pre><code>        class MyList{\n    int id;\n    string date;\n    string email;\n    }\n    \n    List&lt;MyList&gt; ml = new Mylist();\n\nml.Add(new MyList(){\nid = 1;\ndate = &quot;2020/09/06&quot;;\nemail = &quot;zarezadeh@gmailcom&quot;\n});\n\nml.Add(new MyList(){\nid = 2;\ndate = &quot;2020/09/01&quot;;\nemail = &quot;zarezadeh@gmailcom&quot;\n});\n\n List&lt;MyList&gt; New_ml = new Mylist();\n\nforeach (var item in ml)\n                {\n                    if (New_ml.Where(w =&gt; w.email == item.email).SingleOrDefault() == null)\n                    {\n                        New_ml.Add(new MyList()\n                        {\n                          id = item.id,\n     date = item.date,\n               email = item.email\n                        });\n                    }\n                }\n</code></pre>\n'}, {'answer_id': 68057196, 'author': 'Lahiru Gamage', 'author_id': 4991516, 'author_profile': 'https://Stackoverflow.com/users/4991516', 'pm_score': 2, 'selected': False, 'text': '<p>Using <a href="https://www.delftstack.com/howto/csharp/csharp-remove-duplicates-from-list/" rel="nofollow noreferrer"><strong>HashSet</strong></a> this can be done easily.</p>\n<pre><code>List&lt;int&gt; listWithDuplicates = new List&lt;int&gt; { 1, 2, 1, 2, 3, 4, 5 };\nHashSet&lt;int&gt; hashWithoutDuplicates = new HashSet&lt;int&gt; ( listWithDuplicates );\nList&lt;int&gt; listWithoutDuplicates = hashWithoutDuplicates.ToList();\n</code></pre>\n'}, {'answer_id': 68962170, 'author': 'Shravankumar Prajapati', 'author_id': 16772141, 'author_profile': 'https://Stackoverflow.com/users/16772141', 'pm_score': 0, 'selected': False, 'text': '<p>As per remove duplicate, We have to apply below logic so It will remove duplicate in fast ways.</p>\n<pre><code>public class Program\n{\n\n    public static void Main(string[] arges)\n    {\n        List&lt;string&gt; cities = new List&lt;string&gt;() { &quot;Chennai&quot;, &quot;Kolkata&quot;, &quot;Mumbai&quot;, &quot;Mumbai&quot;,&quot;Chennai&quot;, &quot;Delhi&quot;, &quot;Delhi&quot;, &quot;Delhi&quot;, &quot;Chennai&quot;, &quot;Kolkata&quot;, &quot;Mumbai&quot;, &quot;Chennai&quot; };\n        cities = RemoveDuplicate(cities);\n\n        foreach (var city in cities)\n        {\n            Console.WriteLine(city);\n        }\n    }\n\n    public static List&lt;string&gt; RemoveDuplicate(List&lt;string&gt; cities)\n    {\n        if (cities.Count &lt; 2)\n        {\n            return cities;\n        }\n\n        int size = cities.Count;\n        for (int i = 0; i &lt; size; i++)\n        {\n            for (int j = i+1; j &lt; size; j++)\n            {\n                if (cities[i] == cities[j])\n                {\n                    cities.RemoveAt(j);\n                    size--;\n                    j--;\n                }\n            }\n        }\n        return cities;\n    }\n}\n</code></pre>\n'}, {'answer_id': 69658860, 'author': 'Infosunny', 'author_id': 10070007, 'author_profile': 'https://Stackoverflow.com/users/10070007', 'pm_score': 1, 'selected': False, 'text': '<p>Using HashSet:\n<code>list = new HashSet&lt;T&gt;(list).ToList();</code></p>\n'}, {'answer_id': 70162977, 'author': 'Onat Korucu', 'author_id': 10037278, 'author_profile': 'https://Stackoverflow.com/users/10037278', 'pm_score': 1, 'selected': False, 'text': '<p>If you need to compare complex objects, you will need to pass a Comparer object inside the Distinct() method.</p>\n<pre><code>private void GetDistinctItemList(List&lt;MyListItem&gt; _listWithDuplicates)\n{\n    //It might be a good idea to create MyListItemComparer \n    //elsewhere and cache it for performance.\n    List&lt;MyListItem&gt; _listWithoutDuplicates = _listWithDuplicates.Distinct(new MyListItemComparer()).ToList();\n        \n    //Choose the line below instead, if you have a situation where there is a chance to change the list while Distinct() is running.\n    //ToArray() is used to solve &quot;Collection was modified; enumeration operation may not execute&quot; error.\n    //List&lt;MyListItem&gt; _listWithoutDuplicates = _listWithDuplicates.ToArray().Distinct(new MyListItemComparer()).ToList();\n\n    return _listWithoutDuplicates;\n}\n</code></pre>\n<p>Assuming you have 2 other classes like:</p>\n<pre><code>public class MyListItemComparer : IEqualityComparer&lt;MyListItem&gt;\n{\n    public bool Equals(MyListItem x, MyListItem y)\n    {\n        return x != null \n               &amp;&amp; y != null \n               &amp;&amp; x.A == y.A \n               &amp;&amp; x.B.Equals(y.B); \n               &amp;&amp; x.C.ToString().Equals(y.C.ToString());\n    }\n\n    public int GetHashCode(MyListItem codeh)\n    {\n        return codeh.GetHashCode();\n    }\n}\n</code></pre>\n<p>And:</p>\n<pre><code>public class MyListItem\n{\n    public int A { get; }\n    public string B { get; }\n    public MyEnum C { get; }\n\n    public MyListItem(int a, string b, MyEnum c)\n    {\n        A = a;\n        B = b;\n        C = c;\n    }\n}\n</code></pre>\n'}, {'answer_id': 73453803, 'author': 'Hasan Tuna Oruç', 'author_id': 4578450, 'author_profile': 'https://Stackoverflow.com/users/4578450', 'pm_score': 0, 'selected': False, 'text': '<p>I have my own way. I am 2 looping same list for compare list items. And then remove second one.</p>\n<pre><code>            for(int i1 = 0; i1 &lt; lastValues.Count; i1++)\n            {\n                for(int i2 = 0; i2 &lt; lastValues.Count; i2++)\n                {\n                    if(lastValues[i1].UserId == lastValues[i2].UserId)\n                    {\n                        lastValues.RemoveAt(i2);\n                    }\n                }\n            }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/47752', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4541/']