-
-
Save VeldaKiara/c5d98ebca8a71e7889f1aa6c9ad916a9 to your computer and use it in GitHub Desktop.
nums = [0, 0, 1, 1, 2, 2, 3, 3, 4] | |
#using list comprehension to remove duplicates | |
non_dup = [] | |
[non_dup.append(x) for x in nums if x not in non_dup] | |
print(str(non_dup)) | |
print(len(non_dup)) |
Hello @meekg33k, I am happy to participate.
I totally missed that I was supposed to exclusively use a function. I used a list comprehension because of time, I thought it would be faster and compact as compared to using a regular loop.
And yes my solution assumed that there will be no case where the result is null.
Something I have learned from doing this challenge is to ask the right questions and not to base the solution on my assumptions and instructions or requirements are as important as solving the problem.
And as much as I want to make the solution compact I need to account for edge cases.
I will write another solution that accounts for the edge cases and uses a function.
Thank you for the feedback as well as the challenge.
I cannot wait for the next challenge.
Hello @veldakarimi, thanks for participating in Week 1 of Algorithm Fridays. I like that you used Python's list comprehension for your solution.
A few things to note:
null
value. If I passNone
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, if you would like to take a look. Let me know what you think.