# Remove Duplicates from Sorted Array LeetCode Solution

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

## Problem Statement ->

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

The relative order of the elements should be kept the same.

```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,_,_,_,_,_]```

## Remove Duplicates from Sorted Array 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)```

## Remove Duplicates from Sorted Array 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)```

## Remove Duplicates from Sorted Array JavaScript Solution ->

``````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)```

## Remove Duplicates from Sorted Array 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)```

#### Best Time to Buy and Sell Stock II LeetCode Problem

You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On…

#### Longest Palindromic Substring LeetCode Solution

Given a string s, return the longest palindromic substring in s. A palindrome is a string which reads the…

#### Add Two Numbers LeetCode Solution

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order,…

#### Merge k Sorted Lists LeetCode Solution

You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all…

#### Contains Duplicate II LeetCode Solution

Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such…

#### Climbing Stairs LeetCode Solution

You are climbing a staircase. It takes n steps to reach the top. Each time you can…