Remove number from array and shift the remaining ones JavaScript


In this problem statement, our task is to write a function to remove numbers from the array and shift the remaining elements with the help of Javascript functionalities. So we will use basic for loop to remove one element from the array and shift the remaining elements to get the output array.

Understanding the problem statement

The problem statement is saying to create a function which can remove a given number from an array and shift the remaining items to fill the gap of the removed item. For example we have given an array [1, 2, 3, 4, 5] and the number 2, the resultant array would be [1, 3, 4, 5] in which the number 2 has been removed and the remaining items has been shifted to fill the gap.

Logic for the given problem

To solve the given problem we will keep tracking of the current position where we will copy elements to. And whenever we find an element that is equal to the given number we will skip that number and continue iterating the array. If we find an item that is not equal to the given number, we will copy it to the current position and increment the position. If the loop is finished, we will set the array's length to the current position, and eliminate the elements that were not copied.

The function will be able to shift the remaining items so that there are no gaps left in the array by removing the number.

Algorithm - Using filter method

Step 1 − Declare an array of integer numbers from which we have to remove the number.

Step 2 − As we have to show the array by removing a number, filter that number from the above array with the usage of filter method in Javascript.

Step 3 − Show the array after removing the number and shifting the gap from the array.

Code for the algorithm - Using filter method

const arrayOfNumbers = [12, 22, 35, 44, 52, 35, 68];
const arrayWithoutNum = arrayOfNumbers.filter(function (number) {
   return number !== 35;
});

// arrayOfNumbers is unchanged
console.log("Actual Array: ",arrayOfNumbers);
console.log("Array without a number: ", arrayWithoutNum);

Algorithm - Using a for loop

Step 1 − Define an array at the first step.

Step 2 − Use an empty array to store the resultant array by removing a number from the array.

Step 3 − With the help of a for loop iterate through the array and check the condition if the number in the array is not equal to the declared number if it is not equal to that number then push it in the new array.

Step 4 − At the end show the resultant array after removing the number from the array.

Code for the algorithm - Using a for loop

const arrayOfNumbers = [12, 22, 35, 44, 52, 35, 68];
const arrayWithoutNum = [];

for (let i = 0; i < arrayOfNumbers.length; i++) {
   if (arrayOfNumbers[i] !== 35) {
      arrayWithoutNum.push(arrayOfNumbers[i]);
   }
}

// arrayOfNumbers is unchanged
console.log("Actual array of Numbers: ", arrayOfNumbers);
console.log("Array after removing a number: ", arrayWithoutNum);

Complexity

Since we have to loop through all of the integers in the array, the time consumed by the approach is O(n). And the algorithm's space complexity is constant O(1). Because no additional data structure is used.

Conclusion

So for the above code we have used a for loop to search for a number to remove from the array in a Javascript and get the remaining elements by shifting positions of elements. This function has a time complexity of O(n) and space complexity of O(1).

Updated on: 18-May-2023

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements