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.

Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array

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

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 …
Read More

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 …
Read More

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 …
Read More

Leave a Comment

Your email address will not be published.