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
create an incrementing value i and set it to 1
Traverse through the nums array
check for edge cases to avoid going over the range of the array's index
loop through the next ith elements to check if they are equal to the current element
remove any ith element that is equal to the current element
If the ith element is not equal to the current element, increment i
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)