C
C#2y ago
Aksword

❔ Permut array's values

Hello, is there a convenient way to swap array values in their array, for example transforming a {1,2,3,4} into a {1,4,2,3} I dunno. Because when I think of this I believe that a need to create a copy of the first array as a reference before swapping the values, but maybe there is a better way.
5 Replies
TravestyOfCode
You can save the first value in a temp variable, then assign the second value in the first value place, then assign the second location the value in temp.
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
ero
ero2y ago
(array[i1], array[i2) = (array[i2], array[i1]);
(array[i1], array[i2) = (array[i2], array[i1]);
MODiX
MODiX2y ago
thinker227#0000
REPL Result: Success
var xs = new[] {1,2,3,4,5};
(xs[1], xs[2]) = (xs[2], xs[1]);
xs
var xs = new[] {1,2,3,4,5};
(xs[1], xs[2]) = (xs[2], xs[1]);
xs
Result: int[]
[
1,
3,
2,
4,
5
]
[
1,
3,
2,
4,
5
]
Compile: 427.710ms | Execution: 58.419ms | React with ❌ to remove this embed.
Angius
Angius2y ago
Tuple swap FTW
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server