Values appearing exactly twice
Your task
Given an array a of length n where every value is in the range [1, n], some values appear once and some appear exactly twice. Return a sorted list of all values that appear twice. Aim for O(n) time and O(1) extra space (not counting the output).
Examples
find_duplicates(a: list[int]) -> list[int]Example 1
Input
find_duplicates([4, 3, 2, 7, 8, 2, 3, 1])Output
[2, 3]