Given an array of positive integers and two distinct valid indices, return the array with the two elements corresponding to the two indices swapped.

You may choose to use 0-indexing or 1-indexing, but the testcases below will be 0-indexed.

array        m n output
[1,2,3,4]    0 1 [2,1,3,4]
[5,8,9]      0 2 [9,8,5]
[11,13,15,3] 1 2 [11,15,13,3]
[11,13,15,3] 2 1 [11,15,13,3]
[11,15,15,3] 2 1 [11,15,15,3]

This is . Shortest answer in bytes wins. Standard loopholes apply.

  • \$\begingroup\$ Related, related. \$\endgroup\$
    – Leaky Nun
    Commented Jun 22, 2017 at 10:28
  • 2
    \$\begingroup\$ Huh, this may well be a task that many golfing languages have a hard time with but most practical languages find easy. (Lists with mutable elements aren't a common thing for golfing languages to have.) If that is the case, it'll be quite interesting. (The golfing languages will probably still win, though, because they're so much terser they can get away with a more complex algorithm.) \$\endgroup\$
    – user62131
    Commented Jun 22, 2017 at 10:36
  • 8
    \$\begingroup\$ Surprised this probably isn't a dupe, but this challenge is actually creative, since it's a real challenge for many golfing languages out there. \$\endgroup\$ Commented Jun 22, 2017 at 10:45
  • \$\begingroup\$ @LeakyNun I've got downvotes (and even delete votes) like that in the past, don't worry too much about it... \$\endgroup\$ Commented Jun 22, 2017 at 10:46
  • \$\begingroup\$ Can m and n be taken as an array? \$\endgroup\$
    – Okx
    Commented Jun 22, 2017 at 11:23

36 Answers 36


awk, 31 bytes


Try it online!

Takes input in the format

1 2
1 2 3 4

and outputs as

2 1 3 4



The entire program is a missing pattern with an action followed by a pattern with a missing action.

Since a missing pattern runs on each line, the code inside the braces runs for both input lines. The c=$a;$a=$b;$b=c; part swaps the two values at indices a and b (through the temporary variable c). This only has an effect on the second line, since on the first line a and b are not yet defined. The a=$1;b=$2 part defines a to be the first field and b to be the second field, which sets the appropriate values for the first part to run on the second line.

Since a missing action is equivalent to {print}, the pattern prints every line it matches. This pattern in particular is NR>1: that is, print whenever the line number is greater than 1, which happens to be line 2. This runs after the swapping of values has taken place, thus completing the task.


q/kdb+, 17 bytes


{@[x;(|)y;:;x y]}


q){@[x;(|)y;:;x y]}[1 2 3 4;0 1]
2 1 3 4


A q version of the k answer by Simon. Apply the assign : function to x at indices reverse-y with value of x indexed at y. Broken down you can see more clearly:

q)x:1 2 3 4
q)y:0 1
q)x y
1 2
1 0
2 1
q)@[x;(|)y;:;x y]
2 1 3 4

Powershell, 64 49 bytes



At least somewhat better solution.

Also I have no idea why redirecting to $ even works ^^


Japt, 15 bytes


Test it


       :Implicit input of array U and integers V & W
£      :Map over the array (with Y being the index of the current element), replacing each element with...
g      :  The element in the array at index...
Y¶V?W  :    W, if Y equals V
:Y¶W?V :    or V, if Y equals W
:Y     :    or just Y otherwise
       :Implicit output of modified array
  • \$\begingroup\$ Nice technique, looks a little verbose though... but I can't find a way to golf it. \$\endgroup\$ Commented Jun 22, 2017 at 16:52
  • \$\begingroup\$ @ETHproductions: yeah, I'm still convinced there's a shorter way, just haven't had the time to come back and find it and it looks like I won't until tomorrow :( \$\endgroup\$
    – Shaggy
    Commented Jun 22, 2017 at 16:54

V, 10 bytes


Try it online!

This takes input with each number to a line, and indices as program arguments. 1-indexed.


C, 42 bytes (one extra argument on function call)


Try it online

C, 44 bytes

f(a,m,n)int*a;{int t=a[m];a[m]=a[n];a[n]=t;}

Not the answer you're looking for? Browse other questions tagged or ask your own question.