DEV Community

Cover image for Apply Operations to an Array
Muhamad Taufik Satya
Muhamad Taufik Satya

Posted on

Apply Operations to an Array

Optimizing Array Operations in TypeScript

Today, I explore a Daily Leetcode problem number 2640. Apply Operations to an Array using TypeScript that modifies an array based on specific rules:

  1. If two consecutive elements are equal, double the first and set the second to 0.
  2. After processing the array, move all zeroes to the end while keeping the relative order of other elements.

link to the question

Let’s break it down step by step.


Understanding the Code

Function Definition

function applyOperations(nums: number[]): number[] {
Enter fullscreen mode Exit fullscreen mode

The function takes an array of numbers (nums) as input.


Step 1: Iterating Through the Array

  for (let i = 0; i < nums.length - 1; i++) {
Enter fullscreen mode Exit fullscreen mode

We use a for loop to iterate from index 0 to second-last element (nums.length - 1). This ensures that we check each element and its next neighbor.


Step 2: Doubling Consecutive Equal Elements

    if (nums[i] == nums[i + 1]) {
      nums[i] *= 2;
      nums[i + 1] = 0;
    }
Enter fullscreen mode Exit fullscreen mode

If two consecutive elements are equal, we:

  1. Double the first element (nums[i] *= 2)
  2. Set the second element to 0 (nums[i + 1] = 0)

Example Transformation:

Input:  [2, 2, 3, 3, 4]
Step 1: [4, 0, 3, 3, 4]  // (2+2 → 4, set next to 0)
Step 2: [4, 0, 6, 0, 4]  // (3+3 → 6, set next to 0)
Enter fullscreen mode Exit fullscreen mode

Step 3: Moving Zeroes to the End

  return nums.filter(Boolean).concat(nums.filter((num) => !num));
}
Enter fullscreen mode Exit fullscreen mode
  • We use .filter(Boolean) to remove 0s and keep non-zero values.
  • We use .filter(num => !num) to extract all zeroes.

Finally, we concatenate the two arrays to place zeroes at the end.

Example Output:

applyOperations([2, 2, 3, 3, 4]);
// Step 1: [4, 0, 6, 0, 4]
// Step 2: Remove zeros -> [4, 6, 4]
// Step 3: Add zeros at the end -> [4, 6, 4, 0, 0]
Enter fullscreen mode Exit fullscreen mode

Final Optimized Code

function applyOperations(nums: number[]): number[] {
  for (let i = 0; i < nums.length - 1; i++) {
    if (nums[i] == nums[i + 1]) {
      nums[i] *= 2;
      nums[i + 1] = 0;
    }
  }
  return nums.filter(Boolean).concat(nums.filter((num) => !num));
}
Enter fullscreen mode Exit fullscreen mode

Time & Space Complexity Analysis

  • Loop through the array → O(n)
  • Filtering & concatenation → O(n)
  • Overall Complexity: O(n) (linear time complexity)

Conclusion

This function efficiently:

  • Modifies the array in place.
  • Moves all zeroes to the end after processing.
  • Runs in O(n) time complexity, making it optimal for large inputs.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More