Created
April 9, 2021 22:56
-
-
Save Julisam/211f1762def590be0d016bfe1927b758 to your computer and use it in GitHub Desktop.
Algorithm Fridays Code Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
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
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 aNone
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.