Values appearing exactly twice

Write codeMedium25 min

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
Inputfind_duplicates([4, 3, 2, 7, 8, 2, 3, 1])
Output[2, 3]