Skip to content

Instantly share code, notes, and snippets.

@jjfiv
Created October 15, 2020 13:40
Show Gist options
  • Save jjfiv/658e80c3c487ae106bc2a00cb836eeb5 to your computer and use it in GitHub Desktop.
Save jjfiv/658e80c3c487ae106bc2a00cb836eeb5 to your computer and use it in GitHub Desktop.
has_duplicates; filter_duplicates
def has_duplicates(xs):
# Now with dictionaries:
# create a dictionary "seen_already" that is empty.
# loop over every element of xs
# if an element is in seen_already, return True
# else; add it to seen_already
# if we haven't returned True by the end of the function, return False
return False
assert(has_duplicates([1,2]) == False)
assert(has_duplicates([1,1,2]) == True)
assert(has_duplicates([1,2,1]) == True)
assert(has_duplicates(["a", "b", "b"]) == True)
def filter_duplicates(xs):
output = []
# Now, collect only those items you haven't seen_before into output
return output
assert(filter_duplicates([1,2]) == [1,2])
assert(filter_duplicates([1,1,2]) == [1,2])
assert(filter_duplicates([1,2,1]) == [1,2])
assert(filter_duplicates(["a", "b", "b"]) == ["a", "b"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment