Lädt...

🔧 Apply Operations to an Array


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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[] {

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++) {

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;
    }

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)

Step 3: Moving Zeroes to the End

  return nums.filter(Boolean).concat(nums.filter((num) => !num));
}
  • 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]

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));
}

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.
...

🔧 Apply Operations to an Array


📈 30.17 Punkte
🔧 Programmierung

🔧 2460. Apply Operations to an Array


📈 30.17 Punkte
🔧 Programmierung

🔧 Upcoming JavaScript Features: Simplifying Array Combinations with `Array.zip` and `Array.zipKeyed`


📈 29.27 Punkte
🔧 Programmierung

🔧 Turbo Array: Supercharge Your JavaScript Array Operations 🚀


📈 27.55 Punkte
🔧 Programmierung

🔧 Minimum operations required to Sort the Array using following operations


📈 25.84 Punkte
🔧 Programmierung

🐧 Apply Yaml Manifest Using “kubectl apply”


📈 24.75 Punkte
🐧 Linux Tipps

🔧 #2635 Leetcode Apply Transform Over Each Element in Array


📈 22.13 Punkte
🔧 Programmierung

🔧 2818. Apply Operations to Maximize Score


📈 20.42 Punkte
🔧 Programmierung

🐧 Convert Array of Starings to Array of Numbers in JavaScript


📈 19.51 Punkte
🐧 Linux Tipps

🔧 Maximize Array sum by adding multiple of another Array element in given ranges


📈 19.51 Punkte
🔧 Programmierung

🔧 Bubble Sort: Given an Array of Unsorted Items, Return A Sorted Array


📈 19.51 Punkte
🔧 Programmierung

🔧 Array.from VS Array.prototype.map


📈 19.51 Punkte
🔧 Programmierung

🔧 Merge array-like objects by Array.prototype.concat()


📈 19.51 Punkte
🔧 Programmierung

📰 Bash For Loop Array: Iterate Through Array Values


📈 19.51 Punkte
🐧 Unix Server

🔧 Select Element in Array() to a new Array() JavaScript


📈 19.51 Punkte
🔧 Programmierung

🔧 Learn the JavaScript Array.every() and Array.some() methods


📈 19.51 Punkte
🔧 Programmierung

🔧 Array / Array Method / Function


📈 19.51 Punkte
🔧 Programmierung

🕵️ FFmpeg Array Access MXF File Out-of-Array memory corruption


📈 19.51 Punkte
🕵️ Sicherheitslücken

🔧 JavaScript Array Length – How to Find the Length of an Array in JS


📈 19.51 Punkte
🔧 Programmierung

🕵️ FFmpeg Array Access MXF File Out-of-Array Pufferüberlauf


📈 19.51 Punkte
🕵️ Sicherheitslücken

🔧 Generate an Array from given Array according to given conditions


📈 19.51 Punkte
🔧 Programmierung

🔧 #123 — Convert One-Dimensional Array to Two-Dimensional Array


📈 19.51 Punkte
🔧 Programmierung

💾 Microsoft Edge Chakra JIT Array.prototype.reverse Array Type Confusion


📈 19.51 Punkte
💾 IT Security Tools

🔧 JS Sum of an Array – How to Add the Numbers in a JavaScript Array


📈 19.51 Punkte
🔧 Programmierung

⚠️ Microsoft Edge Chakra JIT Array.prototype.reverse Array Type Confusion


📈 19.51 Punkte
⚠️ PoC

🔧 JavaScript Array Tutorial – Array Methods in JS


📈 19.51 Punkte
🔧 Programmierung

🔧 std::array in C++ isn't slower than array in C


📈 19.51 Punkte
🔧 Programmierung

🐧 ES6 Map an Array of Objects to Return an Array of Objects With New Keys


📈 19.51 Punkte
🐧 Linux Tipps

⚠️ [dos] Microsoft Edge Chakra JIT - 'Array.prototype.reverse' Array Type Confusion


📈 19.51 Punkte
⚠️ PoC

🔧 How to Create an Array in Java – Array Declaration Example


📈 19.51 Punkte
🔧 Programmierung

🔧 2022. Convert 1D Array Into 2D Array


📈 19.51 Punkte
🔧 Programmierung

matomo