Trains the technique from
LeetCode 442Find All Duplicates in an ArrayThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A stock take produced nums, the sequence of bin labels a handheld scanner read, one entry per scan. There are n scans in total and every label is between 1 and n. A bin was either scanned once or scanned twice; no bin was scanned three times.
Return the labels of the bins that were scanned twice. The labels may be returned in any order, and each such label appears once in the result.
Do this in time linear in n, and without allocating extra room that grows with n: apart from the list you return, only a constant number of extra values may be held, though you are free to rearrange or overwrite nums itself.
Example 1
Labels 4 and 5 were each read twice, while 6, 8, 7 and 1 were each read once.
Example 2
Both scans read bin 1, so it is the only label scanned twice.
Example 3
Each of the two bins was scanned once, so nothing is reported.
The values you return may be in any order.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def find_duplicates(nums: list[int]) -> list[int]:public List<Integer> findDuplicates(int[] nums)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.