Skip to content

Commit 5346a23

Browse files
authored
Create 2460-apply-operations-to-an-array.js
1 parent c6cff3d commit 5346a23

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

2460-apply-operations-to-an-array.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var applyOperations = function(nums) {
6+
const n = nums.length
7+
for(let i = 0; i < n - 1; i++) {
8+
if(nums[i] === nums[i + 1]) {
9+
nums[i] *= 2
10+
nums[i + 1] = 0
11+
}
12+
}
13+
const res = nums.filter(e => e !== 0)
14+
while(res.length !== n) res.push(0)
15+
return res
16+
};

0 commit comments

Comments
 (0)