Получение уникальных значений

Массивы и коллекции / Массивы

numbers=(1 3 2 1 3)

#sort -u sorts and drops the repeats (-n compares as numbers)
mapfile -t unique < <(printf '%s\n' "${numbers[@]}" | sort -nu)
#unique is (1 2 3)

#uniq drops only the repeats that stand side by side,
#so without sort it changes nothing here
mapfile -t notSorted < <(printf '%s\n' "${numbers[@]}" | uniq)
#notSorted is (1 3 2 1 3)

#in the order of the first occurrence: awk prints a line
#only the first time it sees it
mapfile -t firstOrder < <(printf '%s\n' "${numbers[@]}" | awk '!seen[$0]++')
#firstOrder is (1 3 2)

echo "unique is (${unique[*]})"
echo "notSorted is (${notSorted[*]})"
echo "firstOrder is (${firstOrder[*]})"