Simple approach using list membership:
def remove_duplicates(nums):
result = []
for num in nums:
if num not in result:
result.append(num)
return result
This works but is because in checks the entire list each time.
Faster approach using a set:
def remove_duplicates(nums):
seen = set()
result = []
for num in nums:
if num not in seen:
seen.add(num)
result.append(num)
return result
Set lookup is , making the whole function . The set tracks what we've seen; the list maintains order.