DEV Community

Cover image for Remove duplicates from sorted array
Metete Welete
Metete Welete

Posted on • Edited on

Remove duplicates from sorted array

Remove Duplicates From Sorted Array Leetcode

So in order to remove the duplicates from the sorted array without creating a new array, I had to traverse through the given array and delete any next element that equals the current element

  1. create an incrementing value i and set it to 1

  2. Traverse through the nums array

  3. check for edge cases to avoid going over the range of the array's index

  4. loop through the next ith elements to check if they are equal to the current element

  5. remove any ith element that is equal to the current element

  6. If the ith element is not equal to the current element, increment i

  7. break out of the loop if edge cases are met i.e if i is greater than or equal to the length of the nums array

Using this approach gives you the array in memory without duplicates so you can return the length.

You can view my code in python through Remove Duplicates From Sorted Array

Top comments (0)