Tracking Down the Mystery: Finding the Unique Delivery ID
Here's a fun one. A drone delivery company logs every delivery ID twice — once at takeoff, once at landing. One morning, a drone doesn't come back. You ha...
13 May 2024

Here's a fun one. A drone delivery company logs every delivery ID twice — once at takeoff, once at landing. One morning, a drone doesn't come back. You have an array of IDs where every ID appears twice except one. Find the missing drone's delivery ID.
Each breakfast delivery is assigned a unique ID. When a drone takes off, the ID is added to an array. When it lands, the ID is added again. After breakfast, 99 of 100 drones returned. One is missing. Find its delivery ID from the array.
The IDs aren't sorted or sequential.
The Set approach
Walk through the array. If an ID isn't in the Set, add it. If it's already there, remove it. At the end, the one remaining ID is your answer.
const findUniqueDeliveryId = (ids) => {
const seen = new Set();
for (const id of ids) {
if (seen.has(id)) {
seen.delete(id);
} else {
seen.add(id);
}
}
return [...seen][0];
};
Time: O(n). Space: O(n).
The XOR approach
Here's the clever trick. XOR a number with itself and you get 0. XOR a number with 0 and you get the number back. So if you XOR every element together, all the pairs cancel out and you're left with the unique one.
const findUniqueDeliveryIdXOR = (ids) => {
let unique = 0;
for (const id of ids) {
unique ^= id;
}
return unique;
};
Time: O(n). Space: O(1).
Why XOR wins
Same time complexity, but O(1) space instead of O(n). No allocations, no hash table overhead, no garbage collection pressure. On a large array, that difference is real.
The trade-off: XOR is less obvious to read. If your team isn't comfortable with bitwise operations, the Set version is clearer and still fast enough. Pick based on context.