Skip to content

Commit 34c209d

Browse files
committed
find unique id
1 parent 132980e commit 34c209d

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

find_unique_id.py

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
"""
2+
Given the list of IDs, which contains many duplicate integers and one unique integer, find the unique integer.
3+
https://www.interviewcake.com/question/python/find-unique-int-among-duplicates
4+
"""
5+
6+
import random
7+
8+
9+
def generate_log():
10+
original_list = list(range(1, 11))
11+
return_list = original_list.copy()
12+
return_list.pop(3)
13+
log = original_list + return_list
14+
random.shuffle(log)
15+
return log
16+
17+
18+
def find_unique_id_cycling(log):
19+
"""
20+
time complexity: n^2
21+
space complexity: 1
22+
"""
23+
comparison_count = 0
24+
for checking in log:
25+
found_instances = 0
26+
27+
for comparison in log:
28+
comparison_count += 1
29+
if checking == comparison:
30+
found_instances += 1
31+
32+
if found_instances == 1:
33+
print(comparison_count)
34+
return checking
35+
36+
37+
def find_unique_cycling_reduction(log):
38+
"""
39+
time complexity: (((n-1) / 2) ^ 2) + (n-1) / 2
40+
space complexity: 1
41+
"""
42+
copied_log = log.copy()
43+
comparison_count = 0
44+
45+
while len(copied_log) > 0:
46+
checking = copied_log[0]
47+
for i in range(1, len(copied_log)):
48+
comparison = copied_log[i]
49+
comparison_count += 1
50+
if checking == comparison:
51+
# remove the found ids from the copied list
52+
copied_log.pop(i)
53+
copied_log.pop(0)
54+
break
55+
else:
56+
print(comparison_count)
57+
return checking
58+
59+
60+
def find_unique_dict(log):
61+
"""
62+
time complexity: n
63+
space complexity: n
64+
"""
65+
duplicate_dict = {}
66+
for i in log:
67+
if duplicate_dict.get(i):
68+
duplicate_dict.pop(i)
69+
else:
70+
duplicate_dict[i] = 1
71+
return list(duplicate_dict)[0]
72+
73+
74+
def find_unique_sort(log):
75+
"""
76+
time complexity: nlogn + (n-1)
77+
space complexity: n
78+
"""
79+
sorted_log = sorted(log)
80+
for a in range(len(log)):
81+
try:
82+
if sorted_log[0] == sorted_log[1]:
83+
sorted_log.pop(1)
84+
sorted_log.pop(0)
85+
else:
86+
return sorted_log[a]
87+
except IndexError:
88+
return sorted_log[0]
89+
90+
91+
def find_unique_crazy_xor(delivery_ids):
92+
"""
93+
time complexity: n
94+
space complexity: 1
95+
"""
96+
97+
unique_delivery_id = 0
98+
99+
for delivery_id in delivery_ids:
100+
unique_delivery_id ^= delivery_id
101+
102+
return unique_delivery_id
103+
104+
105+
test_list = [4, 3, 7, 2, 6, 5, 9, 9, 2, 7, 8, 5, 1, 8, 1, 3, 10, 6, 10]
106+
print('cycling beginning of the list: n where n={}'.format(len(test_list)))
107+
print('result: ', find_unique_id_cycling(test_list))
108+
109+
test_list = [3, 7, 2, 6, 5, 9, 9, 2, 7, 8, 5, 1, 8, 1, 3, 10, 6, 10, 4]
110+
print('cycling end of the list: n^2 . where n={}'.format(len(test_list)))
111+
print('result: ', find_unique_id_cycling(test_list))
112+
113+
test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10]
114+
print('cycling end of the palindromic list, n^2 . where n={}'.format(len(test_list)))
115+
print('result: ', find_unique_id_cycling(test_list))
116+
117+
test_list = [4, 3, 7, 2, 6, 5, 9, 9, 2, 7, 8, 5, 1, 8, 1, 3, 10, 6, 10]
118+
print('reduction beginning of the list, n . where n={}'.format(len(test_list)))
119+
print('result: ', find_unique_cycling_reduction(test_list))
120+
121+
test_list = [3, 7, 2, 6, 5, 9, 9, 2, 7, 8, 5, 1, 8, 1, 3, 10, 6, 10, 4]
122+
print('reduction end of the list, random . where n={}'.format(len(test_list)))
123+
print('result: ', find_unique_cycling_reduction(test_list))
124+
125+
test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
126+
print('reduction worst case, (((n-1) / 2) ^ 2) + (n-1) / 2 . where n={}'.format(len(test_list)))
127+
print('result: ', find_unique_cycling_reduction(test_list))
128+
129+
test_list = [1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1]
130+
print('reduction worst case, (((n-1) / 2) ^ 2) + (n-1) / 2 . where n={}'.format(len(test_list)))
131+
print('result: ', find_unique_cycling_reduction(test_list))
132+
133+
print('dict worst case, n where n={}'.format(len(test_list)))
134+
print('result: ', find_unique_dict(test_list))
135+
136+
print('sort worst case, nlog(n) + (n-1) where n={}'.format(len(test_list)))
137+
print('result: ', find_unique_sort(test_list))
138+
139+
print('xor worst case, n where n={}'.format(len(test_list)))
140+
print('result: ', find_unique_crazy_xor(test_list))

0 commit comments

Comments
 (0)