DEV Community

swatiBabber
swatiBabber

Posted on

Remove duplicates from Sorted Array

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

}

Top comments (0)