Find the odd one out in a sequence
Jelly, 15 bytes
⁹R2*‘ṡ5ḟ@€µEÐfQ
TryItOnline
All test cases also at TryItOnline
Returns a list containing one list containing the odd one out.
How?
⁹R2*‘ṡ5ḟ@€µEÐfQ - Main link, a (list)
⁹ - literal 256 (saving a byte over literal 30)
R - range, [1,2,3,...]
2* - 2 ** x, [2,4,8,...]
‘ - increment, [3,5,9,...]
ṡ5 - all contiguous slices of length 5
ḟ@€ - filter with reversed arguments for each
µ - monadic chain separation
Ðf - filter on condition:
E - all equal (those previously filtered lists with only one value)
Q - unique (there can be two, but both will have the same odd-one-out)
JavaScript (ES6), 62 bytes
a=>a.find(n=>--n&--n|!n)||a.sort((a,b)=>a-b)[a[0]*16>a[3]?4:0]
Completely new algorithm, since as @edc65 pointed out the previous one was broken. Explanation: We first deal with the easy case by looking for a 2 or a number that is not one greater than a power of 2. If none was found then there are two possible cases, depending on whether the extra value was below or above the original run of five, so we check whether the smallest and second largest value belong to the same run of five and if so blame the largest value otherwise the smallest value.
Racket 198 bytes
(λ(m)(let((l(for/list((i(range 1 31)))(+ 1(expt 2 i))))(r 1)(n(length m)))(for((i(-(length l)n)))(let
((o(for/list((j m)#:unless(member j(take(drop l i)n)))j)))(when(eq?(length o)1)(set! r o))))r))
Ungolfed version:
(define f
(λ(m)
(let ((l (for/list ((i (range 1 31)))
(+ 1 (expt 2 i))))
(res 1)
(n (length m)))
(for ((i (- (length l) n)))
(let ((o (for/list ((j m)
#:unless (member j
(take (drop l i) n)))
j)))
(when (eq? (length o) 1)
(set! res o))))
res)))
Testing:
(f '(5 9 17 33 829))
(f '(9 5 17 829 33))
(f '(5 9 177 33 65))
(f '(65 129 259 513 1025))
(f '(129 259 513 1025 65))
(f '(63 129 257 513 1025))
(f '(65 129 257 513 4097))
Output:
'(829)
'(829)
'(177)
'(259)
'(259)
'(63)
'(4097)