# Remove leetcode duplicates from sorted arrays

Given an ordered array nums, remove the repeated elements in place so that each element appears only once and return the new length of the deleted array.

Instead of using extra array space, you must modify the input array in place and do so using O(1) extra space.

Description:

Why is the return value an integer, but the output answer an array?

Note that the input array is passed as a “reference,” which means that changes to the input array in the function are visible to the caller.

You can imagine the internal operation as follows:

// Nums is passed by reference. Int len = removeDuplicates(nums)

// Modifying the input array in the function is visible to the caller. // Depending on the length your function returns, it prints out all the elements in the array within that length range. for (int i = 0; i < len; i++) { print(nums[i]); } Example 1:

Input: nums = [1,1,2] Output: 2, nums = [1,2] Explanation: The function should return the new length 2, and the first two elements of the original array nums have been changed to 1,2. You don’t need to worry about elements beyond the new length of the array. Example 2:

Input: nums = [0,0,1,1,1, 1,2,2,3, 4] Output: 5, nums = [0,1,2,3,4] Explanation: The function should return the new length 5, and the first five elements of the original array NUMs are modified to 0,1,2,3,4. You don’t need to worry about elements beyond the new length of the array.

## 1. Find that adjacent elements are not equal

``* @param {number[]} nums * @return {number} */ var removeDuplicates = function(nums) { for(let i=0; i<nums.length; i++){ if(nums[i]==nums[i+1]){ nums.splice(i,1) i--; } } return nums.length };Copy the code``

## 2. Be equal to the value by index

``````var removeDuplicates = function(nums) {
for(let i=0; i<nums.length; i++){if(nums.indexOf(nums[i])! =i){ nums.splice(i,1) i--; }}return nums.length
};
Copy the code``````

## 3. Double pointer (recommended)

``````var removeDuplicates = function(nums) {
if(! nums.length)return 0;
let i = 0;
for(let j = 1; j < nums.length; j++){
if(nums[j] !== nums[i]){
i++;
nums[i] = nums[j];
}
}
return ++i;
};
Copy the code``````