Reduce function that filters out duplicates