Is there a better way for this code? also is there a way to remove duplicates?

Printable View