List 如何去重

 

List<string[]> 如何去重,代碼以下:ide

static void Main(string[] args)
        {

            List<string[]> list = new List<string[]>();

            list.Add(new string[] { "1", "2", "3" });
            list.Add(new string[] { "1" });
            list.Add(new string[] { "1", "2", "3" });
            list.Add(new string[] { "1" });
            list.Add(new string[] { "1" });

            List<string> strList = new List<string>();
            foreach (var item in list)
            {
                string s = string.Join(",", item);
                strList.Add(s);
            }
            //要刪除的元素的下標集合
            List<int> removeIndexList = new List<int>();
            if (list.Count >= 2)  //確保下標i不越界
            {
                string currentStr = string.Empty;
                for (int i = 0; i < strList.Count; i++)
                {
                    currentStr = strList[i];
                    for (int j = i + 1; j < strList.Count; j++)
                    {
                        if (currentStr == strList[j])
                        {
                            //添加到要刪除的索引集合removeIndexList中
                            removeIndexList.Add(j);
                        }
                    }
                }
                removeIndexList = removeIndexList.Distinct().ToList();////去除重複的索引
                //添加到要刪除的對象集合
                List<string[]> removeList = new List<string[]>();
                foreach (var index in removeIndexList)
                {
                    removeList.Add(list[index]);
                }
                //遍歷要刪除對象的集合,刪除原集合中的對象
                foreach (var item in removeList)
                {
                    list.Remove(item);
                }

                foreach (var item in list)
                {
                    string s = string.Join(",", item);
                    Console.WriteLine(s);
                }
                Console.ReadKey();

            }
        }
View Code

 

運行截圖以下:ui

 

 

那麼問題又來了,挖掘機技術……呸! 若是是List<List<string[]>>的集合又該如何去重呢?spa

原理是同樣的把List<string[]>變成字符串,裝到List<string>中,根據List<sting>重複的元素的下標索引,刪除原集合中重複的元素,3d

代碼以下:code

 static void Main(string[] args)
        {
            List<string[]> list = new List<string[]>();

            list.Add(new string[]{"1","2","3"});
            list.Add(new string[] { "1","2" ,"3"});
            list.Add(new string[] { "1" });
            list.Add(new string[] { "1" });
            list.Add(new string[] { "1" });

           

            List<string[]> list2 = new List<string[]>();

            list2.Add(new string[] { "1", "2", "3", "4", "5" });
            list2.Add(new string[] { "1", "2", "3" });
            list2.Add(new string[] { "1" });
            list2.Add(new string[] { "1" });
            list2.Add(new string[] { "1" });

            List<string[]> list3 = new List<string[]>();
            list3.Add(new string[] { "1", "2", "3" });
            list3.Add(new string[] { "1", "2", "3" });
            list3.Add(new string[] { "1" });
            list3.Add(new string[] { "1" });
            list3.Add(new string[] { "1" });


            List<string[]> list4= new List<string[]>();

            list4.Add(new string[] { "1", "2", "3", "4", "5" });
            list4.Add(new string[] { "1", "2", "3" });
            list4.Add(new string[] { "1" });
            list4.Add(new string[] { "1" });
            list4.Add(new string[] { "1" });
            List<List<string[]>> superList = new List<List<string[]>>();
            //集合list和集合list3是相同,list2和list4相同,而且list4添加了2次
            superList.Add(list);
            superList.Add(list2);
            superList.Add(list3);
            superList.Add(list4);
            superList.Add(list4);

            List<string> strList = new List<string>();
            foreach (var d in superList)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var dd in d)
                {
                    string s = string.Join(",", dd);
                    sb.Append(s);
                }
                string str = sb.ToString();
                strList.Add(str); //把superList中每一個子元素拼接成一條字符串放到strList中
            }
           
            //要刪除的元素的下標集合
            List<int> removeIndexList = new List<int>();
            if (strList.Count >= 2) //有2個以上的元素纔有可能出現重複
            {
                string currentStr = string.Empty;
                for (int i =0; i < strList.Count; i++)
                {
                    currentStr = strList[i];
                    for (int j =i+1; j < strList.Count; j++)
                    {
                        if (currentStr == strList[j])
                        {
                            //添加到要刪除的索引集合removeIndexList中
                            removeIndexList.Add(j);
                        }
                    }
                }
            }
            removeIndexList = removeIndexList.Distinct().ToList();//去除重複的索引
            //要刪除的對象集合
            List<List<string[]>> superRemoveList = new List<List<string[]>>();
            foreach (var index in removeIndexList)
            {
                superRemoveList.Add(superList[index]);
            }

            foreach (var item in superRemoveList)
            {
                superList.Remove(item);
            }
            Console.WriteLine(superList.Count());
            Console.ReadKey();
        }
View Code

 

 

運行截圖以下:對象

相關文章
相關標籤/搜索