Remove Duplicates from Sorted Array LeetCode Solution
Remove Duplicates from Sorted Array LeetCode Solution
- 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. Then return the number of unique elements in nums.
Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Remove Duplicates from Sorted Array LeetCode Solution:
Consider the number of unique elements of
nums
to bek
, to get accepted, you need to do the following things:- Change the array
nums
such that the firstk
elements ofnums
contain the unique elements in the order they were present innums
initially. The remaining elements ofnums
are not important as well as the size ofnums
. - Return
k
.
- Change the array
Constraints :
1 <= nums.length <= 3 * 104
-100 <= nums[i] <= 100
nums is sorted in non-decreasing order.
Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
Remove Duplicates from Sorted Array LeetCode Solution:
The code starts iterating from
i = 1
because we need to compare each element with its previous element to check for duplicates.The main logic is inside the
for
loop:- If the current element
nums[i]
is not equal to the previous elementnums[i - 1]
, it means we have encountered a new unique element. - In that case, we update
nums[j]
with the value of the unique element atnums[i]
, and then incrementj
by 1 to mark the next position for a new unique element. - By doing this, we effectively overwrite any duplicates in the array and only keep the unique elements.
Once the loop finishes, the value of
j
represents the length of the resulting array with duplicates removed.Finally, we return
j
as the desired result.- If the current element
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Remove Duplicates from Sorted Array LeetCode Solution:
C++
Java
Python
C++
class Solution { public: int removeDuplicates(vector& nums) { int j = 1; for(int i = 1; i < nums.size(); i++){ if(nums[i] != nums[i - 1]){ nums[j] = nums[i]; j++; } } return j; } };
Java
class Solution { public int removeDuplicates(int[] nums) { int j = 1; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[i - 1]) { nums[j] = nums[i]; j++; } } return j; } }
Python
class Solution: def removeDuplicates(self, nums: List[int]) -> int: j = 1 for i in range(1, len(nums)): if nums[i] != nums[i - 1]: nums[j] = nums[i] j += 1 return j
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment