Trains the technique from
LeetCode 82Remove Duplicates from Sorted List IIThis 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 turnstile writes each badge it reads into a chain of records, one record per read, ordered
so the badge numbers never decrease from the front of the chain to the back. The chain is
handed to you as scans, the badge number of every record in front-to-back order, and an
empty chain arrives as an empty list.
A badge that was read more than once is treated as unusable, so every record carrying that badge number leaves the chain, not just the repeats. Records whose badge number appears exactly once stay where they are.
Return the badge numbers of the records still in the chain, front to back. Do the work by unlinking records from the chain as you walk it once, rather than by tallying the whole chain into a fresh structure first.
Example 1
Badge -3 was read twice and badge 0 three times, so all of those records leave. Badges -1 and 4 were read once each and keep their front-to-back order.
Example 2
Badge 7 appears three times and badge 9 twice, so only the single record for badge 8 remains.
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 keep_singletons(scans: list[int]) -> list[int]:public int[] keepSingletons(int[] scans)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.