We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6ac93fb commit ecd38b3Copy full SHA for ecd38b3
JustDevRae/Set/pick_random_k_numbers.js
@@ -0,0 +1,18 @@
1
+function solution(arr, k) {
2
+ const uniqueSet = new Set();
3
+ const result = [];
4
+
5
+ for (const num of arr) {
6
+ if (!uniqueSet.has(num)) {
7
+ uniqueSet.add(num);
8
+ result.push(num);
9
+ }
10
+ if (result.length === k) break;
11
12
13
+ while (result.length < k) {
14
+ result.push(-1);
15
16
17
+ return result;
18
+}
0 commit comments