Delete duplicates in a List of int arrays

前端 未结 8 1195
我寻月下人不归
我寻月下人不归 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:25

    Input list;

    List> initList = new List>();
    initList.Add(new List{ 0, 0, 0 });
    initList.Add(new List{ 20, 30, 10, 4, 6 });  //this
    initList.Add(new List { 1, 2, 5 });
    initList.Add(new List { 20, 30, 10, 4, 6 });  //this
    initList.Add(new List { 12, 22, 54 });
    initList.Add(new List { 1, 2, 6, 7, 8 });
    initList.Add(new List { 0, 0, 0, 0 });
    

    You can create a result list, and before adding elements you can check if it is already added. I simply compared the list counts and used p.Except(item).Any() call to check if the list contains that element or not.

    List> returnList = new List>();
    
    foreach (var item in initList)
    {
        if (returnList.Where(p => !p.Except(item).Any() && !item.Except(p).Any()
                                 && p.Count() == item.Count() ).Count() == 0)
        returnList.Add(item);
    }
    

提交回复
热议问题