teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コメントの指摘修正

2018/06/27 08:00

投稿

takezoux2
takezoux2

スコア6

answer CHANGED
@@ -10,7 +10,15 @@
10
10
  var item1Set = new HashSet<string>(item1);
11
11
  var item2Set = new HashSet<string>(item2);
12
12
 
13
+ // 要素が増えてくるとHashSetに一旦入れたほうが処理が早くなります
14
+ var allItemSet = new HashSet<string>(item1);
15
+ foreach(var item in item2) {
16
+ allItemSet.Add(item);
17
+ }
18
+
13
19
  foreach( var s in searchlist) {
20
+ if(!allItemSet.Contains(s)) continue;
21
+
14
22
  item1Set.Add(s); // 重複要素は追加されないので、なにもチェックする必要なし
15
23
  item2Set.Add(s);
16
24
  }
@@ -20,8 +28,14 @@
20
28
  };
21
29
 
22
30
  // itemXが複数バージョン
23
- var itemLists = new List<string[]>(){item1, item2, ...}.Select(items => new HashSet<string>(items)).ToList();
31
+ var itemLists = new List<string[]>(){item1, item2, ...}
32
+ .Select(items => new HashSet<string>(items))
33
+ .ToList();
34
+ var allItemSet = new HashSet<string>(itemLists.SelectMany(_ => _));
35
+
24
36
  foreach(var s in serachlist) {
37
+ if(!allItemSet.Contains(s)) continue;
38
+
25
39
  foreach(var itemSet in itemLists) {
26
40
  itemSet.Add(s);
27
41
  }