Easy JS solution explained | O(n) time and O(1) space
Easy JS solution explained | O(n) time and O(1) space\nTC: O(n) and SC: O(1)\n\nvar applyOperations = function(nums) {\n// perform the operation on each array e
# Easy JS solution explained | O(n) time and O(1) space\n**TC: O(n) and SC: O(1)**\n```\nvar applyOperations = function(nums) {\n// perform the operation on each array element\n    for(let i = 0; i < nums.length - 1; i++){\n        if(nums[i] === nums[i + 1]){\n            nums[i] *= 2;\n            nums[i + 1] = 0;\n        }\n    }\n// Move zeroes to end\n    let index = 0;\n    nums.forEach(num => {\n        if(num !== 0)\n            nums[index++] = num;\n    });\n    while(index < nums.length){\n        nums[index++] = 0;\n    }\n    return nums;\n};\n```\n\n**If the interviewer asks you not to alter the input**\n```\nvar applyOperations = function(nums) {\n// perform the operation on each array element\n    for(let i = 0; i < nums.length - 1; i++){\n        if(nums[i] === nums[i + 1]){\n            nums[i] *= 2;\n            nums[i + 1] = 0;\n        }\n    }\n    const res = Array(nums.length).fill(0);\n    let index = 0;\n\t// Copy the non-zero elements to result array\n    nums.forEach(num => {\n        if(num !== 0)\n            res[index++] = num;\n    });\n    return res;\n};\n```
['JavaScript']