質問編集履歴
1
ありがとうございます! 恥ずかしながら、プログラムがかなりの初心者で、HashSet.Contains() と HashSet.RemoveWhere() を使う方法が考えてみたもののわからずじまい
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,9 +5,9 @@
|
|
5
5
|
|
6
6
|
### 該当のソースコード
|
7
7
|
|
8
|
-
```ここに言語名を入力
|
9
|
-
C#
|
8
|
+
```C#
|
10
9
|
|
10
|
+
|
11
11
|
HashSet<int> A = new HashSet<int> { 1, 2, 6 };
|
12
12
|
HashSet<int> B = new HashSet<int> { 6, 8 };
|
13
13
|
HashSet<int> C = new HashSet<int> { 9, 5 };
|
@@ -27,4 +27,70 @@
|
|
27
27
|
|
28
28
|
HashSetで重複しない配列を作れるというのはわかったのですが、
|
29
29
|
判定して結合していくループをどう作ったら良いのかが全くわかりません。
|
30
|
-
ご教授いただければ幸いです。
|
30
|
+
ご教授いただければ幸いです。
|
31
|
+
|
32
|
+
## 新たに試してみたソースコード
|
33
|
+
```C#
|
34
|
+
|
35
|
+
//ランダムな数字を持ったハッシュセット
|
36
|
+
HashSet<int> A = new HashSet<int> { 1, 2, 6 };
|
37
|
+
HashSet<int> B = new HashSet<int> { 6, 8 };
|
38
|
+
HashSet<int> C = new HashSet<int> { 9, 5 };
|
39
|
+
HashSet<int> D = new HashSet<int> { 1, 3, 8 };
|
40
|
+
HashSet<int> E = new HashSet<int> { 7, 9 };
|
41
|
+
HashSet<int> F = new HashSet<int> { 4 };
|
42
|
+
|
43
|
+
//上記のハッシュセットをまとめたリスト
|
44
|
+
List<HashSet<int>> listA;
|
45
|
+
|
46
|
+
//最終でダブりのないハッシュセットをもつリスト
|
47
|
+
List<HashSet<int>> listB;
|
48
|
+
//補佐のリスト
|
49
|
+
List<HashSet<int>> listC;
|
50
|
+
//listBに追加するか否かのフラグ
|
51
|
+
private bool NA = false;
|
52
|
+
|
53
|
+
void Start () {
|
54
|
+
//初期化
|
55
|
+
listA = new List<HashSet<int>> { A, B, C, D, E, F };
|
56
|
+
listB = new List<HashSet<int>> { };
|
57
|
+
listC = new List<HashSet<int>> { };
|
58
|
+
}
|
59
|
+
|
60
|
+
public void Union()
|
61
|
+
{
|
62
|
+
//listAの中身を総当たりして、要素が被ってたら一番最初のハッシュセットにまとめる
|
63
|
+
for (int i = 0; i < listA.Count; i++)
|
64
|
+
{
|
65
|
+
for (int j = 1; j < listA.Count; j++)
|
66
|
+
{
|
67
|
+
if (listA[i].Overlaps(listA[j]))
|
68
|
+
{
|
69
|
+
listA[i].UnionWith(listA[j]);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
NA = true;
|
73
|
+
if(listC.Count!=0){
|
74
|
+
foreach(var k in listC){
|
75
|
+
//すでに要素が被ってるものがある場合はlistBに追加しない
|
76
|
+
if (listA[i].Overlaps(k)){
|
77
|
+
NA = false;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
}
|
81
|
+
if(NA==true){listB.Add(listA[i]);}
|
82
|
+
listC.Add(listA[i]);
|
83
|
+
}
|
84
|
+
|
85
|
+
print("最終チェック"+listB.Count);
|
86
|
+
foreach (var l in listB)
|
87
|
+
{
|
88
|
+
print(l + "の中の数字は");
|
89
|
+
foreach (var m in l)
|
90
|
+
{
|
91
|
+
print(m);
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
```
|