Find the ticket number printed more than once
Your task
A ticket printer should number its tickets from 1 to n. A bug made it print n + 1 tickets, and exactly one number came out on more than one ticket.
Write find_duplicate(nums) that returns the number that repeats.
Input and output:
nums: the n + 1 ticket numbers in print order, where n is from 1 to 100,000. Every value is an integer from 1 to n.- Returns the repeated number.
Rules:
- Exactly one value appears more than once. It may appear 2 or more times, so some numbers from 1 to n can be missing.
- Every other value appears at most once.
Then, in a sentence or two: how would you find it without changing nums and with only a fixed amount of extra memory?
Examples
find_duplicate(nums: list[int]) -> intExample 1
Input
find_duplicate([1, 3, 4, 2, 2])Output
2Example 2
Input
find_duplicate([2, 5, 9, 6, 9, 3, 8, 9, 7, 1])Output
9