Write a Python program to find duplicates in a list using a nested loop, and then optimize it using a dictionary or set.