GAZAR

Principal Engineer | Mentor

Tracking Down the Mystery: Finding the Unique Delivery ID

Tracking Down the Mystery: Finding the Unique Delivery ID

To uncover the unique delivery ID amidst a sea of duplicates, we'll employ a TypeScript solution leveraging index iteration. By iteratively comparing elements in the array, we can pinpoint the unique ID that eludes detection.

Our 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 an array, deliveryIdConfirmations. When the drone comes back and lands, the ID is again added to the same array. 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 array of IDs, which contains many duplicate integers and one unique integer, find the unique integer. The IDs are not guaranteed to be sorted or sequential. Orders aren't always fulfilled in the order they were received, and some deliveries get cancelled before takeoff.

const findUniqueDeliveryId = (ids) => {
    const checked = new Set();
    checked.add(ids[0])    

    for (let i = 1; i < ids.length; i++){
        if (checked.has(ids[i])) {
            checked.delete(ids[i]);
        } else {
            checked.add(ids[i])
        }
    }
    
    return Array.from(checked)[0]
}

const deliveryIds = [123, 456, 789, 123, 456, 789, 987];
const uniqueDeliveryId = findUniqueDeliveryId(deliveryIds);
console.log("Unique Delivery ID:", uniqueDeliveryId); // Output: 987

In the face of mystery and intrigue, our TypeScript solution utilizing index iteration has successfully unveiled the unique delivery ID. By meticulously comparing elements in the array, we've identified the elusive ID that holds the key to resolving the breakfast delivery company's predicament. This algorithmic approach demonstrates the power of TypeScript in solving real-world challenges, ensuring the seamless operation of autonomous drone delivery services. As the breakfast delivery company regains control over its fleet, it stands ready to continue serving customers with unparalleled efficiency and reliability.


Comments