diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 048c61ee..77577fc0 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -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) \ No newline at end of file +- [First in First out Queue](queues/fifo-queue.py) diff --git a/algorithms/Python/arrays/remove_duplicates_list.py b/algorithms/Python/arrays/remove_duplicates_list.py new file mode 100644 index 00000000..e2781352 --- /dev/null +++ b/algorithms/Python/arrays/remove_duplicates_list.py @@ -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)) +