Created
May 6, 2020 13:13
-
-
Save AnjaliManhas/7824d47f39298d0f59def35286bf4d5f to your computer and use it in GitHub Desktop.
Remove Duplicates from Sorted Array LeetCode- Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int removeDuplicates(int[] nums) { | |
int i=0; | |
int j=1; | |
int temp=1; | |
while(j<nums.length) | |
{ | |
if(nums[i]==nums[j]) | |
{ | |
j++; | |
} | |
else | |
{ | |
nums[temp]=nums[j]; | |
temp++; | |
i++; | |
} | |
} | |
return i+1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment