9 of 25
fixed the description of the hash function in the original answer
Arnauld
  • 188.2k
  • 20
  • 171
  • 630

#JavaScript (ES6), 71 bytes

Takes input as an array of numbers. Falsy values are 0 or NaN. Truthy values are strictly positive integers.

a=>a[1]*a.every(p=n=>(373>>n|373>>p)&n+p!=10|a[-p-(p=n)>>1]?a[-n]^=1:0)

###Test cases

let f =

a=>a[1]*a.every(p=n=>(373>>n|373>>p)&n+p!=10|a[-p-(p=n)>>1]?a[-n]^=1:0)

console.log('[Truthy]')
console.log(f([1,2]))
console.log(f([1,6]))
console.log(f([5,8,2]))
console.log(f([1,2,3,5,7]))
console.log(f([1,5,7,8,2]))
console.log(f([4,2,6,3,1]))
console.log(f([1,5,7,8,4,2]))

console.log('[Falsy]')
console.log(f([1]))
console.log(f([1,3]))
console.log(f([3,1]))
console.log(f([3,3]))
console.log(f([1,3,7]))
console.log(f([1,5,4,1]))
console.log(f([1,9,7,3,5]))


#Original answer, 90 bytes

Saved 2 bytes by mixing different falsy/truthy values, as suggested by @HermanLauenstein

Takes input as an array of numbers. Falsy values are 0 or NaN. Truthy values are strictly positive integers.

a=>a[b=1]*a.every(n=>!(p=b,b=1<<n,j=-'085645205005'[(p^b)*60%198%37])|p<2|a[j]?a[-n]^=1:0)

###Test cases

let f =

a=>a[b=1]*a.every(n=>!(p=b,b=1<<n,j=-'085645205005'[(p^b)*60%198%37])|p<2|a[j]?a[-n]^=1:0)

console.log('[Truthy]')
console.log(f([1,2]))
console.log(f([1,6]))
console.log(f([5,8,2]))
console.log(f([1,2,3,5,7]))
console.log(f([1,5,7,8,2]))
console.log(f([4,2,6,3,1]))
console.log(f([1,5,7,8,4,2]))

console.log('[Falsy]')
console.log(f([1]))
console.log(f([1,3]))
console.log(f([3,1]))
console.log(f([3,3]))
console.log(f([1,3,7]))
console.log(f([1,5,4,1]))
console.log(f([1,9,7,3,5]))

###Commented

a =>                       // given the input array a[]
  a[b = 1] *               // test whether a[1] exists; initialize b to 1
  a.every(n =>             // for each digit n in a[]:
    !(p = b,               //   p = previous value of b
      b = 1 << n,          //   b = bitmask with the n-th bit set (0-indexed)
      j = -'085645205005'[ //   find j = negated value of the 'in-between digit'
        (p ^ b)            //     by XOR'ing b with p
        * 60 % 198 % 37    //     and applying the hash function (see below)
      ]                    //   end of table lookup
    ) |                    //   if j is zero
    p < 2 |                //   or p is less than 2 (meaning this is the 1st iteration)
    a[j] ?                 //   or the (-j)-th digit was already visited:
      a[-n] ^= 1           //     mark the n-th digit as visited or fail if it already was
    :                      //   else:
      0                    //     force every() to fail
  )                        // end of every()

###Hash function

We're using the commutative function H() defined by:

H(a, b) = H(b, a) = ((1 << a) ^ (1 << b)) * 60 % 198 % 37

There are 8 combinations of keys that result in an 'in-between key' to test:

 a | b | 1 << a | 1 << b | XOR | * 60  | MOD 198 | MOD 37
---+---+--------+--------+-----+-------+---------+--------
 1 | 3 |     2  |     8  |  10 |   600 |     6   |    6      --> 2
 1 | 7 |     2  |   128  | 130 |  7800 |    78   |    4      --> 4
 1 | 9 |     2  |   512  | 514 | 30840 |   150   |    2      --> 5
 2 | 8 |     4  |   256  | 260 | 15600 |   156   |    8      --> 5
 3 | 7 |     8  |   128  | 136 |  8160 |    42   |    5      --> 5
 3 | 9 |     8  |   512  | 520 | 31200 |   114   |    3      --> 6
 4 | 6 |    16  |    64  |  80 |  4800 |    48   |   11      --> 5
 7 | 9 |   128  |   512  | 640 | 38400 |   186   |    1      --> 8

For all other possible pairs (a, b), H(a, b) doesn't collide with any of the indices computed above.

Arnauld
  • 188.2k
  • 20
  • 171
  • 630