Removes head and tail partial duplicates