0

A question a Hackerrank "Lonely Integer" has the following requirements. Given a set of integer with size N, find integers that are not repeated.

I am a Bash noob. I am finding a lot of trouble with bash, for example, when do I use while (( )) vs while [[ ]], vs while [ ]?

I have been debugging this code for the past 1 hour.... Its taking way too long, this question is not hard at all. I did the question using the most naive way possible which has a run time of O(n^2)

The compiler is complaining

"solution.sh: line 7: [: 1: integer expression expected"

I have tried to change my while loop to while(()), while[[]], while[ ], none of them work!!

My main question is, can someone please point out to me why my code does not work?!

Solutions are not available on hackerrank

Thanks!

read N
read line
declare -a array=( $line )

i=0
k=0
while [ $i -lt $N ]
do
    isunique=1
    j=0
    while [ $j -lt $N ]
    do
        if [[ ${array[$i]} -eq ${array[$j]} ]] && [[ $i -ne $j ]]
        then
            isunique=0
            break
        fi
        j=$((j+1))
    done
    if [ $isunique -eq 1 ]
    then
        unique[$k]=${array[$i]}
        k=$((k+1))
    fi
    i=$((i+1))
done

echo ${unique[@]}
exit 0
Telenoobies
  • 938
  • 3
  • 16
  • 33
  • when to use which brackets? see this one: http://stackoverflow.com/questions/2188199/how-to-use-double-or-single-bracket-parentheses-curly-braces – Sundeep Sep 22 '16 at 02:03
  • to check your bash script for typos and suggestions to improve: http://www.shellcheck.net/ – Sundeep Sep 22 '16 at 02:03
  • can you add sample input you provide to this script, so that others can test it out? – Sundeep Sep 22 '16 at 02:05
  • Quote vars in single `[`, also don't use single `[` if your shell supports `[[`. – 123 Sep 22 '16 at 07:24
  • @JamesBrown I am running it on hackerrank, and it does not work.... But it works on my linux environment... !! weird – Telenoobies Sep 22 '16 at 13:03

0 Answers0