Write a program to find duplicate elements in an array and explain its time complexity.
Implement find_duplicates(nums). Return a list containing each value that appears at least twice, exactly once, ordered by its second occurrence. The input is a list of integers, and the output is a list of integers.
def find_duplicates(nums):