Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution:
def removeDuplicates(self, nums):
#check for cases of an empty array
if len(nums) == 0:
return 0
nextNonDuplicate = 1
i = 1
#since the array is sorted, all duplicates would be adjacent,
#so we start from the first element and check if the next element isn't a duplicate
#if it isn't, we update the array in-place using the index
while i < len(nums):
if nums[nextNonDuplicate - 1] != nums[i]:
nums[nextNonDuplicate] = nums[i]
nextNonDuplicate += 1
i += 1
return nextNonDuplicate
@meekg33k

This comment has been minimized.

Copy link

@meekg33k meekg33k commented Apr 12, 2021

This is a really good solution @KingAshiru and thanks for participating in Week 1 of Algorithm Fridays. I really like that you updated the array in-place making your solution memory-optimal.

The only assumption your solution has made is that the input array cannot have a null value because if I pass a None value as input to your code, it breaks. Ideally, you want to write code that is robust and doesn't fail on edge cases.

I have posted my solution here, let me know what you think.

@KingAshiru

This comment has been minimized.

Copy link
Owner Author

@KingAshiru KingAshiru commented Apr 12, 2021

@KingAshiru

This comment has been minimized.

Copy link
Owner Author

@KingAshiru KingAshiru commented Apr 12, 2021

@meekg33k

This comment has been minimized.

Copy link

@meekg33k meekg33k commented Apr 13, 2021

Awesome and thanks for walking through the feedback @KingAshiru.
See you on Friday!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment