18
\$\begingroup\$

In this challenge you will receive a list of integers as input. Your program should then determine if there are two equal values in the list at an even distance from each other.

This is so the goal is to minimize the size of your source code as measured in bytes.

IO

You may take the array input the standard ways, but just for a little extra fun, you may optionally take the number 2 as an additional input.

Your program or function should output one fixed value if the input satisfies the requirement and a distinct fixed value if it does not.

Test cases

False:

[]
[0]
[1,2]
[1,1]
[10,1]
[2,-1,3]
[1,1,-2]
[1,2,3,4,5,6,7,8]
[1,2,3,1,2,3]

True:

[1,10,1]
[2,2,2]
[1,2,3,4,0,-1,1,8]

This challenge is based on this older challenge by FlipTack. In that challenge one is checking whether there is pair of values at an even distance from each other which are not equal. This is conceptually a minor change, but because equality is transitive and inequality is not, this change thwarts the short approaches to that challenge.

\$\endgroup\$

24 Answers 24

11
\$\begingroup\$

R, 34 31 bytes

\(a)any(ave(a,1:2,FUN=table)>1)

Attempt This Online!

-3 bytes thanks to pajonk.

ave replaces each element by its count in the respective odd- or even-indexed sublists, and a count greater than 1 means there is at least one even distance pair, so we only need to check if any() are greater than 1.

R, 39 bytes

\(a,`+`=anyDuplicated)+a[!1:0]|+a[!0:1]

Attempt This Online!

Checks even-indexed elements for duplicates, then odd-indexed, and checks if either of those is TRUE.

Operator precedence order needed is [, then the alias for anyDuplicated, then finally |, so + is the only alias that will work since ! is already in use.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ -3 bytes on the ave one. \$\endgroup\$ Commented Dec 29, 2025 at 20:09
  • \$\begingroup\$ @pajonk thanks, I did think about unique but forgot about table! \$\endgroup\$ Commented Dec 29, 2025 at 20:29
8
\$\begingroup\$

Jelly, 5 bytes

ŒœQ€Ƒ

Try It Online!

output is inverted — 0 means there is a pair, 1 means there is not

Explanation

ŒœQ€Ƒ    main link
Œœ       split the list into two lists: even- and odd-index elements
    Ƒ    is it invariant (i.e. does nothing change)
  Q€     when we uniquify each list?

if it is invariant, then both lists already contain no duplicates, meaning there are no pairs

-1 thanks to Unrelated String by replacing QƑ€ with Q€Ƒ, removing the need to then combine the results

\$\endgroup\$
4
  • \$\begingroup\$ ŒœQ€Ƒ for 5 perhaps? \$\endgroup\$ Commented Dec 26, 2025 at 19:17
  • 1
    \$\begingroup\$ @UnrelatedString oh that's smart thanks :3 \$\endgroup\$ Commented Dec 26, 2025 at 22:47
  • 1
    \$\begingroup\$ Funnily enough, the version that accepts 2 on the right (and so, generalizes the challenge) is also 5 bytes :P \$\endgroup\$ Commented Dec 28, 2025 at 18:45
  • \$\begingroup\$ @cairdcoinheringaahing there's also ĠḂQ€Ƒ and the dyadic Ġ%Q€Ƒ, both for five too. \$\endgroup\$ Commented Jan 24 at 19:29
8
\$\begingroup\$

Python, 34 bytes

f=lambda l,*L:l in L[1::2]or f(*L)

Attempt This Online!

Signals by exit code: No error means there is a pair. Error means there is no pair.

How?

Recursively cuts the first element off and checks whether it occurs in an odd position of the remainder.

\$\endgroup\$
6
\$\begingroup\$

Google Sheets, 75 bytes

=max(map(A:A,lambda(v,let(d,row(v)-match(v,A:A,),if(len(v),iseven(d)*d)))))

Gets zero for false and the even distance for true.

screenshot

Prettified:

=max(map(A:A, lambda(v, let(
  d, row(v) - match(v, A:A,),
  if(len(v), iseven(d) * d)
))))
\$\endgroup\$
5
\$\begingroup\$

MATL, 11 bytes

,G@QL)Sd~va

Try it at MATL online! Or verify all test cases.

Explanation

,     % Do twice
  G   %   Push input
  @Q  %   Push 1 in the first iteration, 2 in the second
  L   %   Push that level from clipboard L: gives [1 2 j] in the first iteration,
      %   or [2 2 j] in the second. As indices, these are interpreted as "1:2:end"
      %   or "2:2:end" respectively 
  )   %   Apply as indices into the input array
  S   %   Sort
  d   %   Consecutive differences
  ~   %   Logical negation
  v   %   Concatenate stack vertically
  a   %   Any: gives 1 if at least one value is non-zero, and 0 otherwise
      % Implicit end
      % Implicit display
\$\endgroup\$
3
  • \$\begingroup\$ Reshaping with 2e! saves 3 bytes: 2e!Sd~aa \$\endgroup\$ Commented Dec 27, 2025 at 14:16
  • \$\begingroup\$ @beaker Good idea! But it will lead to problems if the input has length two ([1,1] would give 1 instead of 0) or odd length (for example [1,0,2] would also give 1 instead of 0). I'll see if I can fix it with 2 or fewer extra bytes \$\endgroup\$ Commented Dec 27, 2025 at 18:34
  • 2
    \$\begingroup\$ Ah, you're right. I need to golf more ;) \$\endgroup\$ Commented Dec 27, 2025 at 21:59
5
\$\begingroup\$

Nekomata -e, 4 bytes

ĭᵃů,

Attempt This Online!

Output is inverted: True means no even-distance pair, False means one was found.

Explanation:

ĭᵃů, | -e: Output whether a solution exists
-----+-------------------------------------
ĭ    | Uninterleave
 ᵃ , | Are both halves:
  ů  |  Unique?

A non-inverted version is 5 bytes:

ĭᵃᵗůI | -e: Output whether a solution exists
------+-------------------------------------
ĭ     | Uninterleave
 ᵃ  I | Is either half:
  ᵗ   |  Not:
   ů  |   Unique?
\$\endgroup\$
5
\$\begingroup\$

Haskell, 41 bytes

h.zip z
z=0:1:z
h l=[a|a<-l,b<-l,a==b]==l

Try it online!

Haskell doesn't have a syntax for slicing to extract every other element. Instead, we mark the even/odd indexes by zipping the infinite list z=[0,1,0,1,...], and check that the resulting elements is all distinct. Doing so via a list comprehension is shorter than importing for the de-duplication built-in nub

43 bytes

import Data.List
((==)=<<nub).zip z
z=0:1:z

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Retina 0.8.2, 21 bytes

1m`^(.+)$(.*¶.*¶)+\1$

Try it online! Takes input on separate lines but link is to test suite that splits on commas for convenience. Explanation:

1`

Limit the output to 0 or 1.

m`

Allow ^ and $ to match on each line, not just the beginning and end of the input.

^(.+)$

Match a number on its own line.

(.*¶.*¶)+

Match a positive even number of newlines with numbers between. (There can't be any numbers before the first newline because we anchored the match to the end of the line.)

\1$

Match a duplicate of the first number.

\$\endgroup\$
4
\$\begingroup\$

Vyxal 3, 5 bytes

U⎂uI≡

Vyxal It Online!

A port of the Jelly answer (kind of)

Explained

U⎂uI≡­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌­
U      # ‎⁡Uninterleave the input (push input[0, 2, 4, ...] and input[1, 3, 5, ...])
 ⎂u    # ‎⁢Uniquify each
   I   # ‎⁣Re-interleave the two lists
    ≡  # ‎⁤And check for exact equality with the original input
💎

Created with the help of Luminespire.

\$\endgroup\$
1
  • \$\begingroup\$ also: Vyxal 3, 5 bytes: U⎂/u∧ Vyxal It Online! \$\endgroup\$ Commented Dec 29, 2025 at 15:21
3
\$\begingroup\$

JavaScript (ES6), 40 bytes

Returns a Boolean value.

a=>a.some((x,i)=>a.some(y=>i--&&x==y&i))

Try it online!

Commented

a =>             // a[] = input array
a.some((x, i) => // for each value x at index i in a[]:
  a.some(y =>    //   for each value y in a[]:
    i-- &&       //     abort if i = 0 (meaning that x and y are
                 //     the same value); decrement i afterwards
    x == y       //     test whether 1) x and y are equal
    & i          //     and 2) i is now odd
  )              //   end of inner some()
)                // end of outer some()

JavaScript (ES6), 40 bytes

A silly recursive version. It's silly because putting the recursive call within the some() leads to a combinatorial explosion. But it prevents the call from being made when a[] is empty.

f=([p,...a])=>a.some((q,i)=>q==p&i|f(a))

Try it online!

Commented

f = (            // f is a recursive function taking:
  [ p,           //   p = next value from the input array
       ...a ]    //   a[] = remaining values
) =>             //
a.some((q, i) => // for each value q at index i in a[]:
  q == p & i     //   test whether q equals p and i is odd
  | f(a)         //   or f(a) is true
)                // end of some()
\$\endgroup\$
3
\$\begingroup\$

K (ngn/k), 9 bytes

#?:'\2!=:

Try it online!

Returns 2 if there is a pair, and 1 otherwise.

       =:   positions of each value
#?:'\2!     at most one of each arity?
\$\endgroup\$
3
\$\begingroup\$

Pyth, 7 8 bytes

{MI.TcQ2

Try it online!

{MI.TcQ2
     cQ2    Split the input into lists of length 2
   .T       Transpose (creates even-position and odd-position lists)
  I        Check whether invariant under
{M         Deduplicating each list

Edit: We need to use .T, not C, because C truncates away the odd-index positions if the list is odd length. +1 byte.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 7 bytes \$\endgroup\$ Commented Jan 14 at 21:27
2
\$\begingroup\$

Python 3.8 (pre-release),  55   54  52 bytes

-1 byte by using the walrus operator.
-2 bytes by swapping the order of the comparison.

lambda l:any(len(s:=l[i::2])>len({*s})for i in[0,1])

Alternatively, a 46-byte solution is valid if exit code counts as a form of output (1 for false, 0 for true):

f=lambda l:len(s:=l[::2])>len({*s})or f(l[1:])

Even though the error is a RecursionError, the 46-byte solution still works, because the code is guaranteed to find a solution within 2 passes.

Try it online! (52 bytes)
Try it online! (46 bytes)

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 11 bytes

⊙θ⊖№✂θκLθ²ι

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for an even distance pair, nothing if not. Explanation:

 θ          Input list
⊙           Any element satisfies
   №        Count of
          ι Current element in
     θ      Input list
    ✂       Sliced from
      κ     Current index to
        θ   Input list
       L    Length
         ²  Taking alternate elements
  ⊖         Is greater than 1
            Implicitly print

Alternative approach, also 11 bytes:

⊙θ№✂θ﹪κ²κ²ι

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for an even distance pair, nothing if not. Explanation:

 θ          Input list
⊙           Any element satisfies
  №         Count of
          ι Current element in
    θ       Input list
   ✂        Sliced from
      κ     Current index
     ﹪      Modulo
       ²    Literal integer `2`
        κ   To current index
         ²  Taking alternate elements
            Implicitly print
\$\endgroup\$
2
\$\begingroup\$

Maple, 57 bytes

s->ormap(i->nops(i)<>nops({i[]}),[ListTools:-Deal(s,2)]);

ListTools:-Deal(s,2) "deals" the numbers out into two "hands" (lists), containing the odd and even indexed entries. Then the number in each list is compared with the number after conversion to a set. ormap yields true if those numbers are not equal in either list; false otherwise.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 42 bytes

->l{l.group_by{$.=1-$.}.any?{|w,r|r!=r|r}}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 36 bytes

a=>a.some(g=x=>(g[[x,a=!a]]+=g)[40])

Try it online!

On second occur of pair [x,a/*alternatively true and false*/] it becomes undefinedx=>(g[[x,a=!a]]+=g)[40]x=>(g[[x,a=!a]]+=g)[40] lengthing >40

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 5 bytes

ιD€ÙÊ

Try it online or try it online with 2 as additional input or verify all test cases.

Explanation:

ι     # Uninterleave the (implicit) input-list into two parts
      # (or alternatively: uninterleave the second implicit input-list into the
      # first implicit input-integer amount of parts)
 D    # Duplicate this pair of lists
  €Ù  # Uniquify each inner list
    Ê # Check that it's NOT equal to the duplicated pair of lists
      # (after which the result is output implicitly)
\$\endgroup\$
2
\$\begingroup\$

Oracle SQL, 83 bytes

select decode(count(unique mod(rownum,2)||column_value),sum(1),0,1)from xmltable(t)

Oracle SQL, 78 bytes

Approach with nested aggregates

select sign(max(sum(1)-1))from xmltable(t)group by mod(rownum,2)||column_value

Try it online!

PostgreSQL, 98 bytes

select sign(sum(1)-1)from unnest(t)with ordinality as t(x,i)group by x,i%2 order by 1 desc limit 1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

APL(NARS), 28 chars

{∨/{⍵≢∪⍵}¨⍵[⍸a]⍵[⍸∼a←2∣⍳≢⍵]}

Interpreter: NARS2000 (Win32) Version # 0.5.14.11

Input one list of numbers, return one boolean value 0 for false 1 for true. The proposition of the question would be the same of "does the list of elements of index odd some repetition? Or does list of elements of index even some repetition?"

⍵[⍸∼a←2∣⍳≢⍵]

would be the list of elements of ⍵ with index even

⍵[⍸a]

would be the list of elements of ⍵ with index odd

{⍵≢∪⍵}

this would find if in the list there are repetitions, if yes return 1 else 0.

test & how to use:

  )box on
Already ON
  h←{∨/{⍵≢∪⍵}¨⍵[⍸a]⍵[⍸∼a←2∣⍳≢⍵]}
  h¨⍬(,0)(1 2)(1 1)(10 1)(2 ��1 3)(1 1 ¯2)(⍳8)(1 2 3 1 2 3)
┌9─────────────────┐
│ 0 0 0 0 0 0 0 0 0│
└~─────────────────┘
  h¨(1 10 1)(2 2 2)(1 2 3 4 0 ¯1 1 8)
┌3─────┐
│ 1 1 1│
└~─────┘
\$\endgroup\$
1
\$\begingroup\$

Japt, 6 bytes

Output is inverted.

ó
eUmâ

Try it (includes all test cases, footer inverts output)

ó\neUmâ     :Implicit input of array
ó           :Uninterleave
 \n         :Assign to variable U
   e        :Is U equal to
    Um      :  Map U
      â     :    Deduplicate
\$\endgroup\$
1
\$\begingroup\$

Vyxal 3, 10 bytes

U;ƛ⑴_☷Ŀ⍟‹a

Vyxal It Online!

U;ƛ⑴_☷Ŀ⍟‹a­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌­
U;          # ‎⁡Uninterleave: get a list of 2 lists, each containing elements that are a pair distance away
  ƛ    ⍟    # ‎⁢map:
   ⑴_☷      # ‎⁣group by value
      Ŀ     # ‎⁤length of the groups
       ⍟    # ‎⁢⁡flatten
        ‹a  # ‎⁢⁢are any of the lengths more than 1?
💎

Created with the help of Luminespire.

<script type="vyxal3">
U;ƛ⑴_☷Ŀ⍟‹a
</script>
<script>
    args=[["[]"],["[0]"],["[1,2]"],["[1,1]"],["[10,1]"],["[2,-1,3]"],["[1,1,-2]"],["[1,2,3,4,5,6,7,8]"],["[1,2,3,1,2,3]"],["[1,10,1]"],["[2,2,2]"],["[1,2,3,4,0,-1,1,8]"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>

Vyxal 3, 5 bytes

porting other solutions:

U⎂/u∧

Vyxal It Online!

U⎂/u∧­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌­
U      # ‎⁡uninterleave
 ⎂  ∧  # ‎⁢are both halves
  /u   # ‎⁣invariant under unique
💎

Created with the help of Luminespire.

\$\endgroup\$
1
\$\begingroup\$

J-uby, 42 bytes

~:zip&[0,1].cycle|:tally|:any?+(:pop|:<&1)

Try it online! (Note: TiO is stuck on Ruby 2.5 so it doesn't have Enumerable#tally—and ATO's J-uby is broken—so you get a custom bespoke monkey-patched version. The code will work without in Ruby 2.7–3.1.)

Explanation

~:zip & [0,1].cycle | :tally | :any? + (:pop | :< & 1)
~:zip & [0,1].cycle |                                   # Zip the input with [1,0,1,...], then
                      :tally |                          # Tally (returns a Hash that counts like elements, with the elements as keys and the counts as values), then
                               :any? + (:pop | :< & 1)  # Check if any of those counts are greater than 1

J-uby could really use Left/Right operators that would return a version of their argument functions that discard their right/left arguments, respectively. If e.g. < was "left" I could replace :pop| above with :<^ for -2 bytes.

\$\endgroup\$
1
\$\begingroup\$

Pip, 10 bytes

YUWgy=UQ*y

Outputs with reversed truth values: 0 if there is an even-distance pair, and 1 if there isn't.

Attempt This Online!

Explanation

YUWgy=UQ*y
   g        List of command-line arguments
 UW         Unweave into two lists of alternating elements
Y           Yank that value into the y variable
    y=      Is y equal to
      UQ*y  itself with each sublist uniquified?
            0 means a sublist contained a duplicate value, which indicates an
            even-distance pair

Alternately (also 10 bytes):

WVUQ*UWg=g

Attempt This Online!

WVUQ*UWg=g
     UWg    Unweave the list, as above
  UQ*       Uniquify each sublist
WV          Weave the sublists back together
        =g  Is the result equal to the original list?

A fun 11-byte solution (also using reversed truth values):

$&$<SN_MUWg

Attempt This Online!

$&$<SN_MUWg
        UWg  Unweave the list, as above
       M     To each sublist, map this function:
    SN_        Sort in ascending order
  $<           Check whether each element is strictly less than the next one
               A result of 0 means there was a duplicate value
$&           Logical AND of the two results
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.