# Remove Duplicates from Sorted Array

Here, We see Remove Duplicates from Sorted Array problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python etc.

## LeetCode Problem :-

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once.

```Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]```
```Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]```

## C++ Solution ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```class Solution {
public:
int removeDuplicates(vector<int>& nums) {
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
return nums.size();
}
};
```Code language: C++ (cpp)```

## Java Solution->

``````class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
```Code language: Java (java)```

## JavaScript Solution ->

``````/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
let c=1
for(let i=1;i<nums.length;i++){
if(nums[i]==nums[i-1])continue
nums[c]=nums[i]
c++
}
let l=nums.length-1
while(l>=c){
nums.pop()
l--
}
};
```Code language: JavaScript (javascript)```

## Python Solution ->

``````class Solution:
def removeDuplicates(self, nums):
len_ = 1
if len(nums)==0:
return 0
for i in range(1,len(nums)):
if nums[i] != nums[i-1]:
nums[len_] = nums[i]
len_ +=1
return len_
```Code language: Python (python)```

Leetcode answers, Leetcode problems and solutions, Leetcode solution, Leetcode solutions python, Leetcode solution C++, Leetcode solution JavaScript, Leetcode solution Java, Remove Duplicates from Sorted Array leetcode solution, C++, Java, JavaScript, Python

### Best Time to Buy and Sell Stock II

Here, We see Best Time to Buy and Sell Stock II problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python …

### Two Sum LeetCode Solution

Here, We see Two Sum LeetCode problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python etc. with different approach. List …

### Add Two Numbers LeetCode Solution

Here, We see Add Two Numbers problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python etc. with different approach. List …