LeetCode #26 – Remove Duplicates from Sorted Array

Here is the next problem:

Once again, we have a problem that has a large description. Not ideal. However, it was labeled ‘Easy’, so I had to give it a go. Though, judging from the likes and dislikes, I thought that this one would be a bit annoying(spoiler alert: it was). Here are some examples of output:

After you take a look at the explanation, it becomes a lot easier to understand what is going on. At least, it was for me when I was doing this problem. As you may have noticed from the description and the title of the problem, the array is sorted in non-decreasing order, which is what we have to take advantage of here. Pretty much all we have to do is traverse through the array and check the indexes which are the number of duplicates from the end of the array and compare them with the current index, and remove the duplicates. After that, just increment the duplicates integer by 1. Then, just return the duplicates integer. Here is what that looks like:

And here are some examples of my output:

Here are the logistics of my solution:

I am more than satisfied with my results, and it was quite an interesting problem to complete. Anyways, if you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today and that you all have a great day!

By:

Posted in:


Leave a comment