chore(Python): add remove duplicates in list (#711)

pull/712/merge
T.K.Santhosh 2022-03-09 01:28:00 +05:30 committed by GitHub
parent 15d44eddae
commit 75e0a5dc3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -5,6 +5,7 @@
- [Majority Element](arrays/majority_element.py)
- [Rotate Array](arrays/rotate_array.py)
- [Missing Number](arrays/missing_number.py)
- [Remove duplicate items](arrays/remove_duplicates_list.py)
## Linked Lists
- [Doubly](linked_lists/doubly.py)
@ -71,4 +72,4 @@
- [Binary Search Tree](trees/binary_search_tree.py)
## Queues
- [First in First out Queue](queues/fifo-queue.py)
- [First in First out Queue](queues/fifo-queue.py)

View File

@ -0,0 +1,33 @@
"""
Algorithm Type : Array Traversal
Time Complexity : O(n)
Space Complexity : O(1)
"""
sample_case = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
def make_distinct(values: list) -> list:
"""
Remove duplicate elements in an array inplace without creating new array.
Here, we are iterating the list backwards instead of forward because if we
remove elements in an array it will cause some issues.
Note : Wrapped with * are sample.
"""
# *length = 10*
length = len(values)
for index in range(len(values)):
# *index_position = 0 - 10*
# *index_position = -10*
index_position = index - length
if values[index_position] in values[0:index_position]:
values.remove(values[index_position])
return values
if __name__ == "__main__":
print(make_distinct(sample_case))