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 · 2 min read

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.
Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.
Forward Deployed AI Engineering: From Customer Problem to Production Value
A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.
January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.
View the live cohortKeep reading
- The Algorithms Worth Knowing in Code Review: Cutting Big O Without Being Clever
- Elusive Least Occurring Number in an Array
- Solving the Height of a Binary Tree Puzzle
- Harry Potter Discounts: A Code Challenge Algorithm
- Implementing a Task Heap Service in TypeScript
- Text Highlighting Algorithm in TypeScript