DEV Community

Oluwanifemi Latunde
Oluwanifemi Latunde

Posted on

Remove Duplicates from Sorted Array

I had been looking for a means to practice data structures and algorithm problems in a healthy competitive environment and I4G's 10 days of coding challenge gave me just the avenue I needed.

The first challenge was to solve a problem on leetcode to remove duplicates from a sorted array arranged in ascending order.
Below is the link to the challenge

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

I solved the challenge using JavaScript and Python.

My thought process:

We want to count the total number of unique values in the array. So the first unique value should be placed in the index 0, the second in the index 1 up to the last unique value which would be in the last index.

We have two pointers to help loop through the array. One for the left and one for the right side of the array. The one on the right scans through the entire array to check for duplicates while the one on the left tells us at what position to put the unique values. The left also tells us how many unique values we have so far, so we increment it by 1.

Below is the link to my final submission for the challenge: https://leetcode.com/problems/remove-duplicates-from-sorted-array/submissions/

Top comments (0)