Two pointers. 0 ms JS/TS. Beats 100.00%
ApproachTwo pointers.Complexity
Time complexity: O(n)

Space complexity: O(1)
Code
# Approach
Two pointers.

![1.png](https://assets.leetcode.com/users/images/82f26fe0-6213-43f6-b79a-502e3e8671a6_1740789759.9259782.png)


# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
const applyOperations = (a) => {
    const n = a.length;
    let j = 0;
    for (let i = 0; i < n; ++i) {
        if (i < n - 1 && a[i] === a[i + 1]) {
            a[i] *= 2;
            a[i + 1] = 0;
        }
        if (a[i] !== 0) a[j++] = a[i];
    }
    a.fill(0, j);
    return a;
};
```
['Array', 'Two Pointers', 'Simulation', 'TypeScript', 'JavaScript']