Skip to content

Commit 9e3d883

Browse files
committed
Add a new question
1 parent f3b13ae commit 9e3d883

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,3 +2181,44 @@ const isPalindrome = s => {
21812181
---
21822182
21832183
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
2184+
2185+
## 60. Move Zeroes
2186+
2187+
Given an integer array `nums`, move all `0`'s in the array to the end of it while maintaining the relative order of the non-zero elements. Achieve this without copying the array or creating a new array.
2188+
2189+
```js
2190+
const moveZeroes = nums => {
2191+
// Your solution
2192+
};
2193+
2194+
console.log(moveZeroes([0, 1, 0, 3, 12])); // [1, 3, 12, 0, 0]
2195+
console.log(moveZeroes([0, 0, 1])); // [1, 0, 0]
2196+
console.log(moveZeroes([0])); // [0]
2197+
```
2198+
2199+
<details><summary>Solution</summary>
2200+
2201+
```js
2202+
const moveZeroes = nums => {
2203+
const length = nums.length;
2204+
let index = 0;
2205+
2206+
for (let i = 0; i < length; i++) {
2207+
if (nums[i] !== 0) {
2208+
nums[index] = nums[i];
2209+
if (index !== i) {
2210+
nums[i] = 0;
2211+
}
2212+
index++;
2213+
}
2214+
}
2215+
2216+
return nums;
2217+
};
2218+
```
2219+
2220+
</details>
2221+
2222+
---
2223+
2224+
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**

0 commit comments

Comments
 (0)