Skip to content

Instantly share code, notes, and snippets.

@KingAshiru
Last active April 13, 2021 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KingAshiru/80f4d8682d8c762ec5e73e04ea1c2ff8 to your computer and use it in GitHub Desktop.
Save KingAshiru/80f4d8682d8c762ec5e73e04ea1c2ff8 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

KingAshiru commented Apr 12, 2021 via email

@KingAshiru
Copy link
Author

KingAshiru commented Apr 12, 2021 via email

@meekg33k
Copy link

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