Skip to content

Instantly share code, notes, and snippets.

@Julisam
Created April 9, 2021 22:56
Show Gist options
  • Save Julisam/211f1762def590be0d016bfe1927b758 to your computer and use it in GitHub Desktop.
Save Julisam/211f1762def590be0d016bfe1927b758 to your computer and use it in GitHub Desktop.
Algorithm Fridays Code Snippet
def remove_duplicates(input_array):
"""
Given a sorted array containing duplicates this function
returns the length of the array without the duplicates
i.e. the number of unique items in the array
Input: nums=[0,0,1,1,2,2,3,3,4]
Output: 5
# O(n) time, O(1) space complexity
"""
return len(set(input_array))
@meekg33k
Copy link

Hello @Julisam, thank you for participating in Week 1 of Algorithm Fridays.

Your solution works and passes most of the test cases. The only assumption you have made is that the input array cannot have a null value because if I pass a None value to your function, it would break.

Ideally, you always want to write code that is robust and takes care of edge cases and unexpected input.

Also, is there a way we could have solved this problem without using a set data structure? What do you think?

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

@Julisam
Copy link
Author

Julisam commented Apr 17, 2021

Thank you for your feedback.
I'm really grateful.

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