• + 0 comments

    in case if you don't want to use sort | uniq etc. for some reason:

    #!/bin/sh
    
    read dummy
    read -a ARRAY
    FILTER=()
    
    # X (listof X) -> Boolean
    member () {
      for i in ${@:2}; do
        if [[ $1 -eq $i ]]; then
            return 0
        fi
      done
      return 1
    }
    
    # X (listof X) -> (listof X)
    filter () {
      new=()
      for i in ${@:2}; do
        if ! [[ $i -eq $1 ]]; then
            new+=($i)
        fi
      done
      # bash functions don't return values, so we use global vars instead
      FILTER=${new[@]}
    }
    
    # (listof X) -> X
    lonelyint () {
      # () to properly create an array from element
      array=("$@")
      count=0
    
      for current in ${array[@]}; do
        let 'count++'
        rest=${array[@]:$count}
        if member $current $rest; then
            # this filter doesn't really work
            # array=(2 32 2) current=2 => array=(3)
            #new=${array[@]/$current/}
            filter $current ${array[@]}
            lonelyint ${FILTER[@]}
        else
            echo "$current"
            exit 0
        fi
      done
    }
    
    lonelyint ${ARRAY[@]}