-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_dup_from_unsorted.py
More file actions
55 lines (48 loc) · 1.32 KB
/
remove_dup_from_unsorted.py
File metadata and controls
55 lines (48 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Remove duplicates from an Unsorted Doubly Linked List. Bonus: Do not use a intermittent buffer
from doubly_linked_list import DoublyLinkedList
# First soultion use a hashtable buffer to do one sweep
def dedup(unsorted_list):
char_store = {}
for x in unsorted_list:
if char_store.get(x.value) == None:
char_store[x.value] = 1
else:
unsorted_list.remove(x.value)
return unsorted_list
# O(1) memory usage solution
def dedup_smaller(unsorted_list):
for x in range(unsorted_list.count):
unsorted_list[x:].remove(unsorted_list[x])
return unsorted_list
if __name__ == "__main__":
# var = DoublyLinkedList()
# var.add(1)
# var.add(2)
# var.add(3)
# var.add(2)
# var.print_list()
# print("-----")
# var = dedup(var)
# var.print_list()
# print("-----")
# print("-----")
# var = DoublyLinkedList()
# var.add(1)
# var.add(2)
# var.add(2)
# var.add(2)
# var.print_list()
# print("-----")
# var = dedup(var)
# var.print_list()
# print("-----")
# print("-----")
var = DoublyLinkedList()
var.add(1)
var.add(2)
var.add(2)
var.add(2)
var.print_list()
print("-----")
var = dedup_smaller(var)
var.print_list()