-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrone_find_unique_id.py
42 lines (28 loc) · 1.57 KB
/
drone_find_unique_id.py
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
# https://www.interviewcake.com/question/java/find-unique-int-among-duplicates
# Your company delivers breakfast via autonomous quadcopter drones. And something mysterious has happened.
# Each breakfast delivery is assigned a unique ID, a positive integer. When one of the company's 100 drones takes off with a delivery, the delivery's ID is added to a list, delivery_id_confirmations. When the drone comes back and lands, the ID is again added to the same list.
# After breakfast this morning there were only 99 drones on the tarmac. One of the drones never made it back from a delivery. We suspect a secret agent from Amazon placed an order and stole one of our patented drones. To track them down, we need to find their delivery ID.
# Given the list of IDs, which contains many duplicate integers and one unique integer, find the unique integer.
# Example [1,3,2,2,1,4,3] # Answer: 4
# METHOD 1
def find_unique_delivery_id(delivery_ids):
# Loop through the list of IDs
# Add each item to a new set
# if the item being iterated is already in the set, pop it.
# Return the remain (unique) ID in the set
unique_set = set()
for i in delivery_ids:
if i not in unique_set:
unique_set.add(i)
else:
unique_set.remove(i)
for i in unique_set:
return i
print(find_unique_delivery_id([1,3,2,2,1,4,3]))
# METHOD 2
def find_unique_delivery_id2(delivery_ids):
unique_id = 0
for delivery_id in delivery_ids:
unique_id ^= delivery_id
return unique_id
print(find_unique_delivery_id2([1,3,2,2,1,4,3]))