Ctrl K

Numbers Handed Out Twice

Medium
A cloakroom hands out tags numbered 1 to n, where n is the length of the array tickets. Because of a glitch some tags were handed out twice (and some not at all). tickets lists every tag handed out; each number appears at most twice. Return every number that appears exactly twice, in any order. Try to do it in O(n) time and O(1) extra space, not counting the output (you may modify the array itself).
Example 1
tickets=[3, 1, 3, 4, 1, 6] → [3, 1]

Numbers 3 and 1 show up twice, all the others once.

Example 2
tickets=[2, 2] → [2]

Number 2 was handed out twice and number 1 not at all.

Example 3
tickets=[2, 3, 1] → []

All numbers are distinct, so there are no repeats.

Constraints
  • 1 ≤ n = len(tickets) ≤ 10⁵
  • 1 ≤ tickets[i] ≤ n
  • every number appears once or twice
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumAnagram Buckets MediumZero Out the Crosses MediumTree From Pre-Order and In-Order Walks MediumTree From In-Order and Post-Order Walks Medium
loading Python…
Input
tickets=[3, 1, 3, 4, 1, 6]
Expected
[3, 1]

Run checks the first 3 tests in your browser. Submit checks all 54 tests on the server, hidden ones included.