Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a7a8c3d

Browse files
committedDec 31, 2021
Add solution #26
1 parent f678e70 commit a7a8c3d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
22|[Generate Parentheses](./0022-generate-parentheses.js)|Medium|
2828
23|[Merge k Sorted Lists](./0023-merge-k-sorted-lists.js)|Hard|
2929
24|[Swap Nodes in Pairs](./0024-swap-nodes-in-pairs.js)|Medium|
30+
26|[Remove Duplicates from Sorted Array](./0026-remove-duplicates-from-sorted-array.js)|Easy|
3031
27|[Remove Element](./0027-remove-element.js)|Easy|
3132
28|[Implement strStr()](./0028-implement-strstr.js)|Easy|
3233
29|[Divide Two Integers](./0029-divide-two-integers.js)|Medium|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 26. Remove Duplicates from Sorted Array
3+
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array nums sorted in non-decreasing order, remove the
7+
* duplicates in-place such that each unique element appears only once.
8+
* The relative order of the elements should be kept the same.
9+
*
10+
* Since it is impossible to change the length of the array in some languages,
11+
* you must instead have the result be placed in the first part of the array
12+
* nums. More formally, if there are k elements after removing the duplicates,
13+
* then the first k elements of nums should hold the final result. It does not
14+
* matter what you leave beyond the first k elements.
15+
*
16+
* Return k after placing the final result in the first k slots of nums.
17+
*
18+
* Do not allocate extra space for another array. You must do this by modifying
19+
* the input array in-place with O(1) extra memory.
20+
*/
21+
22+
/**
23+
* @param {number[]} nums
24+
* @return {number}
25+
*/
26+
var removeDuplicates = function(nums) {
27+
let i = 0;
28+
for (let j = 0; j < nums.length; j++) {
29+
if (nums[i] !== nums[j]) {
30+
nums[++i] = nums[j];
31+
}
32+
}
33+
return ++i;
34+
};

0 commit comments

Comments
 (0)
Please sign in to comment.