-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection-sort.js
More file actions
43 lines (38 loc) ยท 1.01 KB
/
collection-sort.js
File metadata and controls
43 lines (38 loc) ยท 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @desc problem : ๊ณตํต์์ ๊ตฌํ๊ธฐ
* @desc site : Olympiad
* @desc level: 3
* @desc solution : ํฌ ํฌ์ธํฐ
*/
/**
* solution
* @param {array} arrayA : ์ซ์๋ฐฐ์ด A
* @param {array} arrayB : ์ซ์๋ฐฐ์ด B
*/
function solution(arrayA, arrayB) {
const answer = [];
let posA = 0;
let posB = 0;
arrayA.sort(function (a, b) {
return a - b;
});
arrayB.sort(function (a, b) {
return a - b;
});
while (posA < arrayA.length && posB < arrayB.length) {
//๋ ์ค ํ์ชฝ ๋ฐฐ์ด์ ๋ชจ๋ ํ์ํ๋ค๋ฉด ๋น๊ต๋์ ์์ผ๋ฏ๋ก ์ข
๋ฃ
if (arrayA[posA] === arrayB[posB]) {
answer.push(arrayA[posA]);
posA++;
posB++;
} else if (arrayA[posA] < arrayB[posB]) {
//์์ ๊ฐ์ด ์ํ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ์ฆ๊ฐ
posA++;
} else {
posB++;
}
}
return answer;
}
const answer = solution([1, 3, 9, 5, 2], [3, 2, 5, 7, 8]);
console.log(answer); //2,3,5