Consider a Array of N-1 elements which has values from 1 to N which means one of the element is always missing. So, In this Blog, sharing the code for the same.
For Example a=( 4 3 2 5) , here number of elements is N-1 which means 4 and values ranges from 1 to N(which means 5) and hence we need to print that missing element or number is 1.
arr=(4 3 2 5) ## Array of 4 elements(N-1)
len_arr_1=$(( ${#arr[@]} +1)) ##Making length equal to N (5)
len_arr=${#arr[@]} ## Traversing all elements of an Array
i=1
k=0
while [ $i -le $len_arr_1 ]
do
j=0
while [ $j -lt $len_arr ]
do
if [ $i -eq ${arr[j]} ]
then
echo $i " is present"
k=$i ## This assignment will help to identify which element or number is not present
break
fi
j=$(( $j + 1))
done
if [ $k -ne $i ] ##Checking which number is not present
then
echo $i " is not present"
fi
i=$(( $i + 1))
done