use sort first to make dups adjacent