Write a Python program to find duplicates in a list using a nested loop, and then optimize it using a dictionary or set.
Implement def find_duplicates(nums):, returning each duplicated value once, in the order it is first detected as a duplicate. The input is a list of integers, and the output is a list of integers.
def find_duplicates(nums):