2017-10-29 @sunderls

https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example, Given input array

`nums = [1,1,2]`

,Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

because we cannot create a new array, we have to do splice inline.

Loop through each member, if found same to previous one, splice it.

to do this, we can shift & push.

```
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
//push a end flag
nums.push(null)
var prev = null;
var current = nums.shift();
while (current !== null){
if (current !== prev) {
nums.push(current);
}
prev = current;
current = nums.shift();
}
return nums;
};
```

this is very simple, and it passed ranking the fastest with 128ms. (thought I don't know whether it is ok to use shift and push);

如果觉得有帮助到你的话，

欢迎支付宝donate