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 0a5ad98 commit a77f298Copy full SHA for a77f298
Relative Sort Array/code.js
@@ -0,0 +1,29 @@
1
+/*
2
+ * @param {number[]} arr1
3
+ * @param {number[]} arr2
4
+ * @return {number[]}
5
+ */
6
+var relativeSortArray = function(arr1, arr2) {
7
+ const hash = {};
8
+ const remaining = []
9
+ for(let n of arr2){
10
+ hash[n] = hash[n] >= 0 ? hash[n] + 1 : 0
11
+ }
12
+
13
+ for(let n of arr1){
14
+ hash[n] = hash[n] >= 0 ? hash[n] + 1 : undefined
15
+ if(hash[n] === undefined) remaining.push(n)
16
17
18
+ remaining.sort((a,b)=>a-b)
19
20
+ for(let i = 0; i < arr2.length; i++){
21
+ const curr = arr2[i]
22
+ if(hash[curr]-- > 1){
23
+ arr2.splice(i,0,curr)
24
+ i--
25
26
27
+ return arr2.concat(remaining)
28
+ };
29
0 commit comments