Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
Created May 6, 2020 13:13
Show Gist options
  • Save AnjaliManhas/7824d47f39298d0f59def35286bf4d5f to your computer and use it in GitHub Desktop.
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.
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