Write a program to solve a classic array manipulation problem, such as finding duplicates or reversing sub-arrays.
For this task, implement find_duplicates(nums), which returns each value that appears exactly twice in nums, in the order its second occurrence is encountered. The input contains integers from 1 through n, where n is the array length, and the function may modify nums.
Input: A list of integers nums.
Output: A list containing each duplicated value once.
Examples: nums = [4, 3, 2, 7, 8, 2, 3, 1] returns [2, 3]; nums = [1, 1, 2, 2, 3] returns [1, 2].
Constraints: 1 <= len(nums) <= 10^4; every value is between 1 and n; each value appears once or twice.
def find_duplicates(nums):