File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 27
27
22|[ Generate Parentheses] ( ./0022-generate-parentheses.js ) |Medium|
28
28
23|[ Merge k Sorted Lists] ( ./0023-merge-k-sorted-lists.js ) |Hard|
29
29
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|
30
31
27|[ Remove Element] ( ./0027-remove-element.js ) |Easy|
31
32
28|[ Implement strStr()] ( ./0028-implement-strstr.js ) |Easy|
32
33
29|[ Divide Two Integers] ( ./0029-divide-two-integers.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments