回答編集履歴
1
バグ修正とI/F合わせ
answer
CHANGED
@@ -1,33 +1,41 @@
|
|
1
1
|
順序がつくもので、量が多ければ双方ソートして頭からせーのっていうのが一番速い気がします(気がするだけ)。
|
2
2
|
※AもBもuniqueを仮定
|
3
|
+
※I/F合わせてソートするオブジェクトも変わりました
|
4
|
+
※Aが空になったときにオーバーフローするバグを修正しました
|
3
5
|
|
4
6
|
```JavaScript
|
7
|
+
function compareFunc_XXX(left, right) {
|
8
|
+
if (left[0] < right[0]) return -1
|
9
|
+
if (left[0] > right[0]) return 1
|
10
|
+
return 0;
|
11
|
+
}
|
12
|
+
|
5
|
-
function
|
13
|
+
function getUnique_XXX(a,b) {
|
14
|
+
const aa = a.map(v => [`${v.x}:${v.y}`,v]);
|
15
|
+
const bb = b.map(v => [`${v.x}:${v.y}`,v]);
|
6
|
-
const sortA =
|
16
|
+
const sortA = aa.sort(compareFunc_XXX);
|
7
|
-
const sortB =
|
17
|
+
const sortB = bb.sort(compareFunc_XXX);
|
8
18
|
let idxA = 0;
|
9
19
|
let idxB = 0;
|
10
20
|
const lenA = a.length;
|
11
21
|
const lenB = b.length;
|
22
|
+
const resA = [];
|
23
|
+
const resB = [];
|
12
24
|
|
13
25
|
while (idxA != lenA || idxB != lenB) {
|
14
|
-
if (idxB == lenB || sortA[idxA] < sortB[idxB]) {
|
26
|
+
if (idxB == lenB || (idxA != lenA && sortA[idxA][0] < sortB[idxB][0])) {
|
15
|
-
resA.push(sortA[idxA]);
|
27
|
+
resA.push(sortA[idxA][1]);
|
16
28
|
++idxA;
|
17
|
-
} else if (idxA == lenA || sortA[idxA] > sortB[idxB])
|
29
|
+
} else if (idxA == lenA || sortA[idxA][0] > sortB[idxB][0]) {
|
18
|
-
resB.push(sortB[idxB]);
|
30
|
+
resB.push(sortB[idxB][1]);
|
19
31
|
++idxB;
|
20
32
|
} else {
|
21
33
|
++idxA;
|
22
34
|
++idxB;
|
23
35
|
}
|
24
36
|
}
|
37
|
+
return [resA, resB];
|
25
38
|
}
|
26
|
-
const a = [0,1,2,3];
|
27
|
-
const b = [2,3,4,5];
|
28
|
-
|
39
|
+
```
|
29
|
-
const d = [];
|
30
|
-
hoge(a,b,c,d);
|
31
40
|
|
32
|
-
```
|
33
|
-
結果
|
41
|
+
測ってみた結果はうちの環境だと、1000個で4と同じくらい、10000個で3に迫ります。
|