Skip to content

Commit 0506e41

Browse files
committed
무작위로_K개의_수_뽑기 / 기초
1 parent cbac4ce commit 0506e41

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function solution(arr, k) {
2+
var answer = [];
3+
for (let i = 0; i < arr.length; i++) {
4+
if (!answer.includes(arr[i])) {
5+
answer.push(arr[i]);
6+
}
7+
}
8+
9+
if (answer.length > k) {
10+
return answer.slice(0, k);
11+
} else if (answer.length < k) {
12+
while (answer.length !== k) {
13+
answer.push(-1);
14+
}
15+
return answer;
16+
} else {
17+
return answer;
18+
}
19+
}
20+
21+
console.log(solution([0, 1, 1, 2, 2, 3], 5));
22+
console.log(solution([0, 1, 1, 1, 1], 4));

0 commit comments

Comments
 (0)