I have a variable that contains the following space separated entries.
variable=\"apple lemon papaya avocado lemon grapes papaya apple avocado mango banana\"
This pipeline version works by preserving the original order:
variable=$(echo "$variable" | tr ' ' '\n' | nl | sort -u -k2 | sort -n | cut -f2-)