
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Remove number from array and shift the remaining ones JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument.
The function should, if the number specified by second argument exists in the array, remove it and shift all the elements right to it one place left. The only condition is that we cannot use the Array methods like slice(), splice and others.
If there exists more than one instances of the number in the array, we should remove the first one.
For example −
If the input array is −
const arr = [3, 5, 6, 3, 7, 8, 8, 6]; const num = 7;
Then the array should become −
const output = [3, 5, 6, 3, 8, 8, 6];
Example
const arr = [3, 5, 6, 3, 7, 8, 8, 6]; const num = 7; const removeElement = (arr = [], num) => { let index = arr.indexOf(num); if(index === -1){ return; }; while(index + 1 < arr.length){ arr[index] = arr[index + 1]; arr[index + 1] = arr[index] - arr[index + 1]; arr[index] = arr[index] - arr[index + 1]; ++index; }; arr.pop(); }; removeElement(arr, num); console.log(arr);
Output
And the output in the console will be −
[ 3, 5, 6, 3, 8, 8, 6 ]
- Related Articles
- JavaScript Array shift()
- Array shift() in JavaScript
- JavaScript Remove random item from array and then remove it from array until array is empty
- How to remove the last digit of a number and execute the remaining digits in JavaScript?
- Remove smallest number in Array JavaScript
- How to remove certain number elements from an array in JavaScript
- Shift last given number of elements to front of array JavaScript
- Remove elements from array using JavaScript filter - JavaScript
- Remove/ filter duplicate records from array - JavaScript?
- Remove element from array referencing spreaded array in JavaScript
- Remove elements from array in JavaScript using includes() and splice()?
- Remove number properties from an object JavaScript
- Shift certain array elements to front of array - JavaScript
- Write the importance of shift() method in javascript array?
- Search by id and remove object from JSON array in JavaScript

Advertisements