
- 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
In-place Algorithm to Move Zeros to End of List in JavaScript
Suppose we are given an array of integers, lets say arr. We are required to write a function that puts all the zeros to the back of the array by modifying the list in-place.
The function should do this in such a way that the relative ordering of other elements should stay the same.
For example −
If the input array is −
const arr = [0, 11, 0, 22, 67];
Then the array should be modified to −
const output = [11, 22, 67, 0, 0];
Example
Following is the code −
const arr = [0, 11, 0, 22, 67]; const moveZeroToEnd = (arr = []) => { const swap = (array, ind1, ind2) => { const temp = array[ind1]; array[ind1] = array[ind2]; array[ind2] = temp; }; let j = 0; for (let i = 0; i < arr.length; ++ i) { if (arr[i] !== 0) { swap(arr, i, j++); } } while (j < arr.length) { arr[j++] = 0; }; }; moveZeroToEnd(arr); console.log(arr);
Output
Following is the console output −
[11, 22, 67, 0, 0]
- Related Articles
- In-place Move Zeros to End of List in Python
- Move All the Zeros to the End of Array in Java
- Move all zeros to the front of the linked list in C++
- Move all zeros to start and ones to end in an Array of random integers in C++
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- Move first element to end of a given Linked List in C++
- Move all zeroes to end of the array using List Comprehension in Python
- Move all zeroes to end of array in C++
- How to move all the zeros to the end of the array from the given array of integer numbers using C#?
- Algorithm to dynamically populate JavaScript array with zeros before and after values
- Why did hunter-gatherers move place to place?
- How to place cursor position at end of text in text input field using JavaScript?
- JavaScript program for Minimum move to end operations to make all strings equal
- Minimum move to end operations to make all strings equal in C++
- Python Program to move numbers to the end of the string

Advertisements