This feels like a homework problem.

sort -u will likely be part of the answer.