Wrapped addToSet in a loop and fixed the comparision function