Skip to content

Instantly share code, notes, and snippets.

@ellisandrews
Last active August 20, 2020 04:09
Show Gist options
  • Save ellisandrews/f4c39526e0333bae6660210a3dd5a148 to your computer and use it in GitHub Desktop.
Save ellisandrews/f4c39526e0333bae6660210a3dd5a148 to your computer and use it in GitHub Desktop.
Polynomial time complexity
from typing import Any, List, Set
def find_duplicates(list_: List[Any]) -> Set[Any]:
"""Find all duplicate items in a list."""
# Initialize a set to hold the duplicate items
duplicates = set()
# Check each item in the list against every other item in the list
for index_1, item_1 in enumerate(list_):
for index_2, item_2 in enumerate(list_):
if index_1 != index_2 and item_1 == item_2:
duplicates.add(item_1)
# Return the set of duplicate items
return duplicates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment