Minimum Index of all Characters in a String

In this blog, Sharing code to find Minimum Index value of all Characters present in a String. Consider a String “allabouttechnologies”.So, mininum Index for l is 1 not 2 and for a, its 0 not 3.

str="alllabouttechnologies"
declare -A dict1 ##declare a dictionary
unset dict1 ##resetting dictionary
newstr='' ## creating Blank String
n=${#str}
i=0
while [[ $i -lt $n ]]
do
Char=${str:$i:1}
echo ${newstr}
CharCntInNewStr=echo ${newstr} | grep ${Char} | wc -l ##If Character is not present, inserting into dictionary Index and Character itself
if [[ $CharCntInNewStr -eq "0" ]]
then
dict1[${i}]=${Char}
echo ${dict1[${i}]}
newstr=${newstr}${str:$i:1}
fi
i=$(( $i + 1))
done
for key in "${!dict1[@]}"; do
echo "$key ${dict1[$key]}"
done


Related posts