DEV Community

loading...

Remove duplicates from Sorted Array

swatibabber profile image swatiBabber ・1 min read

If array is a sorted array, use two pointer approach to find the position of non duplicate items in the array.

public class Solution {
public int RemoveDuplicates(int[] nums)
{
int len=0;
if(nums.Length==0)
{ return 0; }

    for(int i=0;i<nums.Length-1;i++)
     {
       if( nums[i]!=nums[i+1])
       {
           len=len+1;
           nums[len]=nums[i+1];
       }
     }
   return len+1;
}
Enter fullscreen mode Exit fullscreen mode

}

Discussion (0)

pic
Editor guide