1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace ConsoleApplication2
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13
14 List<int> list1 = new List<int> { 1, 2, 3 };
15 List<int> list2 = new List<int> { 4, 5, 6 };
16 List<int> list3 = new List<int> { 1, 4, 7 };
17
18
19 List<int> list4 = new List<int> { 10, 11, 12 };
20 List<int> list5 = new List<int> { 13, 11, 14 };
21
22
23 List<int> list6 = new List<int> { 16, 17, 18 };
24 List<int> list7 = new List<int> { 18, 19, 20 };
25
26 List<List<int>> list = new List<List<int>>();
27 list.Add(list1);
28 list.Add(list2);
29 list.Add(list3);
30 list.Add(list4);
31 list.Add(list5);
32 list.Add(list6);
33 list.Add(list7);
34
35 List<int> allint = new List<int>();//所有的集合数据
36 HashSet<int> repeated = new HashSet<int>(); //得到没有重复的hashset
37 foreach (List<int> item in list)
38 {
39 foreach (int index in item)
40 {
41 if (allint.Contains(index))
42 repeated.Add(index);//得到所有重复的集合的元素
43 allint.Add(index);//得到所有的集合的元素
44 }
45 }
46
47
48 foreach (var setkey in repeated)//循环重复的值
49 {
50 List<int> templist = null;//临时
51 List<List<int>> removelist = new List<List<int>>();
52 foreach (var item in list)//循环
53 {
54 //if (templist == null)
55 //{
56 // templist = item;
57 // removelist.Add(item);
58 //}
59
60 //if (item.Contains(setkey))
61 //{
62 // removelist.Add(item);
63 // templist = templist.Union(item).ToList();
64 //}
65
66 if (item.Contains(setkey))
67 {
68 if (templist == null)
69 {
70 templist = item;
71 removelist.Add(item);
72 }
73 else
74 {
75 removelist.Add(item);
76 templist = templist.Union(item).ToList();
77 }
78 }
79 }
80 foreach (var item in removelist)
81 {
82 list.Remove(item);
83 }
84 removelist.Clear();
85 list.Add(templist);
86 }
87
88 foreach (var item in list)
89 {
90 foreach (var item1 in item)
91 {
92 Console.Write(item1 + " , ");
93 }
94 Console.Write("\r\n");
95 }
96 Console.ReadKey();
97 }
98
99
100 }
101 }