|
|
发表于 2023-4-1 10:14:35
|
显示全部楼层
本帖最后由 coolloves 于 2023-4-1 10:20 编辑
a=(14 25 39 46 46 52)
target=46
declare -A map
for i in "${!a[@]}"; do
if [ ${map[${a[$i]}]+_} ]; then
map["${a[$i]}"]="${map[${a[$i]}]},$i"
else
map["${a[$i]}"]=$i
fi
done
if [ ${map[$target]+_} ]; then
echo "Target $target found at index ${map[$target]}"
else
echo "Target $target not found in array"
fi
先转成字典.之后查询字典即可.
为了考虑a中有重复数值,做个判断,把这个值的所有位置汇总下
遍历一次是不可避免的,多次判断才有意义
本人专属,你瞅啥? |
|