Last Car in the Garage
A small parking garage logs a car's ticket number twice, once when the car drives in and once when it drives out. The log is one long list in no useful order.
At closing time exactly one car is still inside, so exactly one ticket appears only once. The gate computer can read the log once but has almost no spare memory.
Your task
Write carStillInside(log). Every ticket number in log appears exactly twice, except one that appears exactly once. Return that number.
Aim for one pass and O(1) extra memory: no map, no set, no sorted copy.
carStillInside(log: int[]) → int
Examples
log = [512, 87, 340, 87, 512]
340
Cars 512 and 87 both came in and went out, so each appears twice. Ticket 340 appears only once, so that car is still inside.
log = [9, 4, 4, 2, 9]
2
The two 4s are neighbours, but the two 9s are not, so you cannot assume that matching tickets sit together. The ticket that appears once is 2.
Constraints
- 1 ≤ log.length ≤ 30,001, and log.length is odd
- 0 ≤ log[i] ≤ 1,000,000,000
- Exactly one number appears once. Every other number appears exactly twice.
Start with brute force
Counting tickets in a hash map takes O(n) time but stores up to n / 2 numbers, which the gate computer cannot afford. Checking each ticket against every other one uses no extra memory but costs O(n²), which is about 900 million comparisons for 30,001 entries.
Sorting the log and then comparing neighbours in steps of two uses little memory, but it costs O(n log n) and needs a sortable copy or permission to reorder the log.
Hints
0 of 4 shown- Hint 1
What is
x XOR xfor any numberx? Andx XOR 0? - Hint 2
XOR of a number with itself is 0, and XOR with 0 changes nothing. XOR also ignores order:
a ^ b ^ aequalsa ^ a ^ b. - Hint 3
So what if you XOR every ticket in the log together? Each pair cancels, wherever its halves sit. What is left?
- Hint 4
Start with
result = 0and XOR in every number. The pairs cancel, and the one ticket with no partner is left.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
XOR compares two numbers bit by bit and gives 1 where the bits differ. Two facts do the work: x ^ x = 0 and x ^ 0 = x. Order does not matter, so every pair cancels however far apart its halves are.
Trace [9, 4, 4, 2, 9] in binary:
0 ^ 9=0000 ^ 1001=1001(9).9 ^ 4=1001 ^ 0100=1101(13).13 ^ 4=1101 ^ 0100=1001(9). The 4 has cancelled.9 ^ 2=1001 ^ 0010=1011(11).11 ^ 9=1011 ^ 1001=0010(2). The 9 cancels too, so the answer is 2.
The middle values 13 and 11 look meaningless, but only the final value matters.
function carStillInside(log) {
let result = 0;
for (const ticket of log) {
result ^= ticket; // pairs cancel: x ^ x = 0
}
return result;
}def car_still_inside(log):
result = 0
for ticket in log:
result ^= ticket # pairs cancel: x ^ x = 0
return resultRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.