Skip to content

Commit aad8ad2

Browse files
committed
refactor code for remove duplicate
1 parent 5212626 commit aad8ad2

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@
33
ItemType = TypeVar("ItemType")
44

55

6-
def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
7-
"""
8-
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
6+
# def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
7+
# """
8+
# Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
99

10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
13-
"""
14-
unique_items = []
10+
# Time complexity:O(n²) Quadratic The outer loop runs n times, and for each value, the inner loop also runs up to n times.
11+
# Space complexity:O(n) store n elements in worst possible case
12+
# Optimal time complexity: O(n) can be improved useing set
13+
# """
14+
# unique_items = []
1515

16-
for value in values:
17-
is_duplicate = False
18-
for existing in unique_items:
19-
if value == existing:
20-
is_duplicate = True
21-
break
22-
if not is_duplicate:
23-
unique_items.append(value)
16+
# for value in values:
17+
# is_duplicate = False
18+
# for existing in unique_items:
19+
# if value == existing:
20+
# is_duplicate = True
21+
# break
22+
# if not is_duplicate:
23+
# unique_items.append(value)
24+
25+
# return unique_items
2426

25-
return unique_items
27+
def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
28+
unique_element= set() # for unique element
29+
order_element =[] # to order maintain
30+
for value in values:
31+
if value not in unique_element:
32+
unique_element.add(value)
33+
order_element.append(value)
34+
return order_element

0 commit comments

Comments
 (0)