Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 990c9ff

Browse files
committed
Add a question
1 parent 580779b commit 990c9ff

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

‎README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ const isValid = s => {
13341334

13351335
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
13361336

1337-
## Reverse Integer
1337+
## 41. Reverse Integer
13381338

13391339
Given a signed 32-bit integer `x`, return `x` with its digits reversed. If reversing `x` causes the value to go outside the signed 32-bit integer range [-2<sup>31</sup>, 2<sup>31</sup> - 1], then return 0.
13401340

@@ -1367,3 +1367,42 @@ const reverse = x => {
13671367
---
13681368

13691369
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
1370+
1371+
## 42. Remove Duplicates from Sorted Array
1372+
1373+
Given a sorted array `nums`, write a function that removes the duplicates in-place such that each element appears only once and returns the new length. Do **not** allocate extra space for another array, you must do this by modifying the input array in-place with `O(1)` extra memory.
1374+
1375+
```js
1376+
const removeDuplicates = nums => {
1377+
// Your solution
1378+
};
1379+
1380+
console.log(removeDuplicates([1, 1, 2])); // 2 (because [1, 2] has length 2)
1381+
console.log(removeDuplicates([0, 1, 1, 1, 2, 2, 3, 3, 4])); // 5
1382+
console.log(removeDuplicates([])); // 0
1383+
```
1384+
1385+
<details><summary>Solution</summary>
1386+
1387+
```js
1388+
const removeDuplicates = nums => {
1389+
if (nums.length <= 1) return nums.length;
1390+
let currentIndex = 0;
1391+
1392+
for (let i = 1; i < nums.length; i++) {
1393+
if (nums[currentIndex] !== nums[i]) {
1394+
currentIndex++;
1395+
nums[currentIndex] = nums[i];
1396+
}
1397+
}
1398+
1399+
nums.length = currentIndex + 1;
1400+
return nums.length;
1401+
};
1402+
```
1403+
1404+
</details>
1405+
1406+
---
1407+
1408+
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /