Remove duplicates from list of object

后端 未结 5 1966
悲&欢浪女
悲&欢浪女 2021-01-20 11:52

I have MyObject with field: id, a, b, c, e, f and I have List with 500 000 items, now how can I remove all duplicate items with of the same value of the parameter a, c, f? <

5条回答
  •  遥遥无期
    2021-01-20 12:23

    If what you are looking for is speed, and don't mind using up some memory then I would recommend that you use a HashSet, if you are interested in doing some custom comparison, then you can make an IEqualityComparer, something like this:

    var original = new ArrayList(); // whatever your original collection is 
    var unique = new HasSet(new MyCustomEqualityComparer());
    
    foreach(var item in original)
    {
        if(!unique.Contains(item))
            unique.Add(item);
    }
    
    return unique;
    

    the issue here is that you may end up gobbling up twice the original memory.

    Update:

    I made some extra research and I think you can achieve just what you want by simply doing:

    var original // your original data
    var unique = new HashSet(origin, new CustomEqualityComparer());
    

    that should take care of removing duplicated data as no duplication is allowed in a HashSet. I'd recommend that you also take a look at this question about GetHasCode implementation guidelines.

    If you want to know some more about the HashSet class follow these links:

    About HashSet
    About IEqualityComparer constructor
    IEqualityComparer documentation

    hope this helps

提交回复
热议问题