Skip to content

Commit d662367

Browse files
authored
Update 2007-find-original-array-from-doubled-array.js
1 parent 13ea9ab commit d662367

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2007-find-original-array-from-doubled-array.js

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/**
2+
* @param {number[]} changed
3+
* @return {number[]}
4+
*/
5+
const findOriginalArray = function(changed) {
6+
const n = changed.length, res = [], { abs } = Math
7+
if(n % 2 === 1 || n === 0) return res
8+
const hash = {}
9+
for(let e of changed) {
10+
if(hash[e] == null) hash[e] = 0
11+
hash[e]++
12+
}
13+
const keys = Object.keys(hash)
14+
keys.sort((a, b) => abs(a) - abs(b))
15+
16+
for(let k of keys) {
17+
if(hash[k] > (hash[k * 2] || 0)) return []
18+
for(let i = 0; i < hash[k]; i++) {
19+
res.push(k)
20+
hash[2 * k]--
21+
}
22+
}
23+
24+
return res
25+
};
26+
27+
// another
28+
129
/**
230
* @param {number[]} changed
331
* @return {number[]}

0 commit comments

Comments
 (0)