Delete duplicates in a List of int arrays

前端 未结 8 1196
我寻月下人不归
我寻月下人不归 2020-12-30 00:25

having a List of int arrays like:

List intArrList = new List();
intArrList.Add(new int[3] { 0, 0, 0 });
intArrList.Add(new int[5] {         


        
8条回答
  •  既然无缘
    2020-12-30 01:14

    You can use a HashSet. HashSet is a collection used for guarantee uniqueness and you can compare items on collection, Intersect, Union. etc.

    Pros: No duplicates, easy to manipulate groups of data, more efficient Cons: You can't get a specific item in the collection, for example: list[0] doesn't work for HashSets. You can only Enumerating the items. e.g. foreach

    Here is an example:

    using System;
    using System.Collections.Generic;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                HashSet> intArrList = new HashSet>(new HashSetIntComparer());
                intArrList.Add(new HashSet(3) { 0, 0, 0 });
                intArrList.Add(new HashSet(5) { 20, 30, 10, 4, 6 });  //this
                intArrList.Add(new HashSet(3) { 1, 2, 5 });
                intArrList.Add(new HashSet(5) { 20, 30, 10, 4, 6 });  //this
                intArrList.Add(new HashSet(3) { 12, 22, 54 });
                intArrList.Add(new HashSet(5) { 1, 2, 6, 7, 8 });
                intArrList.Add(new HashSet(4) { 0, 0, 0, 0 });
    
                // Checking the output
                foreach (var item in intArrList)
                {
                    foreach (var subHasSet in item)
                    {
                        Console.Write("{0} ", subHasSet);
                    }
    
                    Console.WriteLine();
                }            
    
                Console.Read();
            }
    
            private class HashSetIntComparer : IEqualityComparer>
            {
                public bool Equals(HashSet x, HashSet y)
                {
                    // SetEquals does't set anything. It's a method for compare the contents of the HashSet. 
                    // Such a poor name from .Net
                    return x.SetEquals(y);
                }
    
                public int GetHashCode(HashSet obj)
                {
                    //TODO: implemente a better HashCode
                    return base.GetHashCode();
                }
            }
        }
    }
    
    
    Output:
    0
    20 30 10 4 6
    1 2 5
    12 22 54
    1 2 6 7 8
    

    Note: Since 0 is repeated several times, HashSet considers the 0 only once. If you need diferentiate between 0 0 0 0 and 0 0 0 then you can replace HashSet> for HashSet> and implement a Comparer to the List instead.

    You can use this link to learn how to compare a list: https://social.msdn.microsoft.com/Forums/en-US/2ff3016c-bd61-4fec-8f8c-7b6c070123fa/c-compare-two-lists-of-objects?forum=csharplanguage

    If you want to learn more about Collections and DataTypes this course is a perfect place to learn it: https://app.pluralsight.com/player?course=csharp-collections&author=simon-robinson&name=csharp-collections-fundamentals-m9-sets&clip=1&mode=live

提交回复
热议问题