Vastly more efficient way of removing duplicate values